jQuery 多个小部件可以在一个元素上具有相同的类吗?
我创建了一个验证 jQuery 小部件来验证表单字段。我希望能够在某些字段上附加两个(或多个)验证规则,例如
$('#textField').validation({type: 'minLength', extraParameters: {length: '4',},});
$('#textField').validation({type: 'maxLength', extraParameters: {length: '20',},});
在字段“textField”上设置最小和最大长度规则。
似乎将多个小部件附加到同一类会使对象被覆盖。即使用页面下方的代码创建对象时会发出以下警报: 做了一个minLength 设置了 maxLength
但当“Validation.validateAll();”时称为页面警报:
验证类型为:maxLength 验证类型为:maxLength
,即第二个对象已覆盖第一个对象的选项。
是否可以在同一个对象上创建同一个小部件的多个实例,而我只是做错了? 或者这是不可能的,我应该使用不同的方法?
干杯 Dan
验证类的代码是:
var Validation = {
pageValidations: [],
validateAll: function(){
for(var i=0; i<Validation.pageValidations.length ; i++){
validation = Validation.pageValidations[i];
validation.validate();
}
},
setErrorMessage: function(errorString, parameters){
var errorElementID = "" + this.element.context.name + "Error";
$("#" + errorElementID).show();
$("#" + errorElementID).text(errorString);
},
clearErrorMessage: function() {
var errorElementID = "" + this.element.context.name + "Error";
$("#" + errorElementID).hide();
},
validateMaxLength: function() {
try{
var stringFromInput = this.element.context.value;
var maxLength = this.options.extraParameters.length;
if(stringFromInput.length > maxLength){
this.setErrorMessage("Length of field is too long.");
return false;
}
}
catch(error){
alert("Exception in validateMinLength, " + error);
}
return true;
},
validateMinLength: function() {
try{
var stringFromInput = this.element.context.value;
var minLength = this.options.extraParameters.length;
if(stringFromInput.length < minLength){
this.setErrorMessage("Length of field is too short.");
return false;
}
}
catch(error){
alert("Exception in validateMinLength, " + error);
}
return true;
},
validate: function (){
var validationResult = false;
alert("Validating type is: " + this.options.type);
switch(this.options.type){
case('minLength'):
validationResult = this.validateMinLength();
break;
case('maxLength'):
validationResult = this.validateMaxLength();
break;
}
if(validationResult == false){
this.options.isFailed = true;
}
return validationResult;
},
valueChanged: function (event){
try{
if(this.options.isFailed != false){
var isValid = this.validate();
if(isValid == true){
this.clearErrorMessage();
}
}
}
catch(error){
alert("Exception in valuechanged " + error);
}
},
_create: function() {
//alert("create called");
},
_init: function() {
this.element.bind('change', jQuery.proxy(this, 'valueChanged'));
$(this.element).addClass('validation');
if(this.options.type == 'minLength'){
alert("made a minLength ");
}
if(this.options.type == 'maxLength'){
alert("made a maxLength ");
}
Validation.pageValidations.push(this);
},
options: {
type: false,
isFailed: false,
extraParameters: {}
}
};
$.widget("dialogue.validation", Validation); // create the widget
function setupValidationFields(){
var object1 = $('#someMaxLength').validation({type: 'minLength', extraParameters: {length: '4',},});
var object2 = $('#someMaxLength').validation({type: 'maxLength', extraParameters: {length: '20',},});
I've created a validation jQuery widget to validate form fields. I want to be able to attach two (or more) validation rules some fields e.g.
$('#textField').validation({type: 'minLength', extraParameters: {length: '4',},});
$('#textField').validation({type: 'maxLength', extraParameters: {length: '20',},});
which sets both a minimum and maximum length rule on the field 'textField'.
It seems that attaching multiple widgets with the same class makes the objects get overwritten. i.e. when creating the objects with the code below the page alerts with:
made a minLength
made a maxLength
but then when "Validation.validateAll();" is called the page alerts with:
Validating type is: maxLength
Validating type is: maxLength
i.e. the second object has overwritten the options of the first object.
Is it possible to make multiple instance of the same widget on the same object and I'm just doing it wrong?
Or is this not possible and I should use a different approach?
cheers
Dan
The code for the validation class is:
var Validation = {
pageValidations: [],
validateAll: function(){
for(var i=0; i<Validation.pageValidations.length ; i++){
validation = Validation.pageValidations[i];
validation.validate();
}
},
setErrorMessage: function(errorString, parameters){
var errorElementID = "" + this.element.context.name + "Error";
$("#" + errorElementID).show();
$("#" + errorElementID).text(errorString);
},
clearErrorMessage: function() {
var errorElementID = "" + this.element.context.name + "Error";
$("#" + errorElementID).hide();
},
validateMaxLength: function() {
try{
var stringFromInput = this.element.context.value;
var maxLength = this.options.extraParameters.length;
if(stringFromInput.length > maxLength){
this.setErrorMessage("Length of field is too long.");
return false;
}
}
catch(error){
alert("Exception in validateMinLength, " + error);
}
return true;
},
validateMinLength: function() {
try{
var stringFromInput = this.element.context.value;
var minLength = this.options.extraParameters.length;
if(stringFromInput.length < minLength){
this.setErrorMessage("Length of field is too short.");
return false;
}
}
catch(error){
alert("Exception in validateMinLength, " + error);
}
return true;
},
validate: function (){
var validationResult = false;
alert("Validating type is: " + this.options.type);
switch(this.options.type){
case('minLength'):
validationResult = this.validateMinLength();
break;
case('maxLength'):
validationResult = this.validateMaxLength();
break;
}
if(validationResult == false){
this.options.isFailed = true;
}
return validationResult;
},
valueChanged: function (event){
try{
if(this.options.isFailed != false){
var isValid = this.validate();
if(isValid == true){
this.clearErrorMessage();
}
}
}
catch(error){
alert("Exception in valuechanged " + error);
}
},
_create: function() {
//alert("create called");
},
_init: function() {
this.element.bind('change', jQuery.proxy(this, 'valueChanged'));
$(this.element).addClass('validation');
if(this.options.type == 'minLength'){
alert("made a minLength ");
}
if(this.options.type == 'maxLength'){
alert("made a maxLength ");
}
Validation.pageValidations.push(this);
},
options: {
type: false,
isFailed: false,
extraParameters: {}
}
};
$.widget("dialogue.validation", Validation); // create the widget
function setupValidationFields(){
var object1 = $('#someMaxLength').validation({type: 'minLength', extraParameters: {length: '4',},});
var object2 = $('#someMaxLength').validation({type: 'maxLength', extraParameters: {length: '20',},});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用的另一种方法是让您的验证小部件在初始化程序中接收规则列表,如下所示:
也就是说,我认为您应该使用优秀的 验证插件 已经存在。
An alternative approach you could use is having your validation widget receive a list of rules in the initialiser, something like the following:
That said, I think you should just use the excellent validation plugin that's already there.