Javascript jQuery-多字段模糊,javascript,validation,jquery,Javascript,Validation,Jquery,我正在使用jQuery进行一些表单验证,在blur上验证每个字段。除了我分组(依赖)字段之外,所有的工作都很好。我要寻找的是一种只在所有字段都模糊后验证这些字段的方法。它们可以分组为jQuery对象的集合,也可以分组为包含元素的子元素
例如,生日输入由三个元素组成:
<fieldset>
<label for="bday_month">Birthday:</label>
<select name="userBirthday[month]"
元素组成:
<fieldset>
<label for="bday_month">Birthday:</label>
<select name="userBirthday[month]" id="bday_month">
<option value="0">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
...
</select>
<select name="userBirthday[day]" id="bday_day">
<option value="0">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
...
</select>
<select name="userBirthday[year]" id="bday_year">
<option value="0">Year</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
...
</select>
</fieldset>
请提前感谢,并让我知道更多代码/解释是否有帮助。将模糊事件仅绑定到最后一个字段。您可以使用类似于'fieldset:input:last'
的选择器来访问它,而无需太多麻烦。这样,您就可以安全地对分组的元素进行制表,直到最后一个元素模糊,表单才会检查错误。将模糊事件仅绑定到最后一个字段。您可以使用类似于'fieldset:input:last'
的选择器来访问它,而无需太多麻烦。这样,您就可以安全地通过分组元素进行制表,直到最后一个元素被模糊,表单才会检查错误。终于找到了工作。我将尽我所能在这里概述它
blur()
事件附加到每个字段
函数off ofblur()
调用setTimeout(函数(){fieldBlurHelper(e);},100)代码>
在fieldBlurHelper()
中,我检查分组字段中是否有任何字段当前处于焦点状态,该类应用于具有焦点的每个元素:
$field.filter('.hasFocus')
如果所有字段都没有焦点,我将运行验证程序
完整(简化)字段模糊功能:
fieldBlur = function(e){
// Array of objects w/specific validation functions, arguments, etc.
validators = $(this).data('validators');
// Process each validator separately
for(key in validators){
validator = validators[key];
$field = validator.$field;
// Extracts the value from grouped fields as an array
val = valHelper($field);
// Call one of the pre-defined validation functions
functionResponse = eval(validator.options.functionName + "(val, validator.options.functionArgs);");
if(!functionResponse){
validator.$error.find('.text').text(validator.options.errorMsg);
validator.$info.hide();
validator.$error.show();
e.preventDefault();
break; // Only display the first error
} else {
validator.$error.hide();
}
}
return true;
};
fieldBlur = function(e){
fieldBlurHelper = function(e){
// Array of validation data (function name, args, etc.)
validators = $(e.target).data('validators');
for(key in validators){
validator = validators[key];
// $field contains all the dependent fields (determined on ready())
$field = validator.$field;
// If any of the dependent fields have focus, don't bother with validation
if($field.filter('.hasFocus').length > 0){ break; }
// Extracts value as an array for all the dependent fields (.val() only returns the first)
val = valHelper($field);
functionResponse = eval(validDater.options.functionName + "(val, validDater.options.functionArgs);");
if(!functionResponse){
console.log('error!');
break; // we only want to show the user one error at a time
} else {
console.log('valid!');
}
}
};
// running the function after the timeout allows the fields to lose focus
setTimeout(function(){ fieldBlurHelper(e); }, 100);
};
终于有办法了。我将尽我所能在这里概述它
blur()
事件附加到每个字段
函数off ofblur()
调用setTimeout(函数(){fieldBlurHelper(e);},100)代码>
在fieldBlurHelper()
中,我检查分组字段中是否有任何字段当前处于焦点状态,该类应用于具有焦点的每个元素:
$field.filter('.hasFocus')
如果所有字段都没有焦点,我将运行验证程序
完整(简化)字段模糊功能:
fieldBlur = function(e){
// Array of objects w/specific validation functions, arguments, etc.
validators = $(this).data('validators');
// Process each validator separately
for(key in validators){
validator = validators[key];
$field = validator.$field;
// Extracts the value from grouped fields as an array
val = valHelper($field);
// Call one of the pre-defined validation functions
functionResponse = eval(validator.options.functionName + "(val, validator.options.functionArgs);");
if(!functionResponse){
validator.$error.find('.text').text(validator.options.errorMsg);
validator.$info.hide();
validator.$error.show();
e.preventDefault();
break; // Only display the first error
} else {
validator.$error.hide();
}
}
return true;
};
fieldBlur = function(e){
fieldBlurHelper = function(e){
// Array of validation data (function name, args, etc.)
validators = $(e.target).data('validators');
for(key in validators){
validator = validators[key];
// $field contains all the dependent fields (determined on ready())
$field = validator.$field;
// If any of the dependent fields have focus, don't bother with validation
if($field.filter('.hasFocus').length > 0){ break; }
// Extracts value as an array for all the dependent fields (.val() only returns the first)
val = valHelper($field);
functionResponse = eval(validDater.options.functionName + "(val, validDater.options.functionArgs);");
if(!functionResponse){
console.log('error!');
break; // we only want to show the user one error at a time
} else {
console.log('valid!');
}
}
};
// running the function after the timeout allows the fields to lose focus
setTimeout(function(){ fieldBlurHelper(e); }, 100);
};
这主意不错。如果用户在表单中向后切换,则不起作用。当然,他们使用鼠标,但我真的很喜欢它的简单性!这主意不错。如果用户在表单中向后切换,则不起作用。当然,他们使用鼠标,但我真的很喜欢它的简单性!