using Jquery to show one error message at a time
I'm trying to show one error message at a time and then once that error has been corrected to show the next error.
For example there is validation on three textbox (Date, Month & Year) once the date textbox is valid then the month error message choudl show.
The code I have so far for this is:
var pi_ArrayValidationResult;
var pi_EachValidator = function(delegate) {
if (typeof (Page_Validators) !== undefined) {
for (var i = 0; i < Page_Validators.length; ++i) {
delegate(Page_Validators[i]);
}
}
};
var pi_CheckValidators = function() {
var fields = {};
pi_EachValidator(function(val) {
if (fields[val.controltovalidate] === undefined) {
fields[val.controltovalidate] = true;
}
fields[val.controltovalidate] = fields[val.controltovalidate] && val.isvalid;
});
for (var field in fi开发者_运维技巧elds) {
if (fields.hasOwnProperty(field)) {
$('#' + field)
.parent('div.fm-req')
.toggleClass('fm-error', !fields[field]);
This section is the bit that needs modification, to show one error then the next once the previous error is corrected:
$('#' + field).siblings('span').hide();
$('#' + field).siblings('span').each(function() {
var $this = $(this);
if (!$this.isvalid) {
$this.show();
return false;
}
});
}
}
};
return {
init: function(args) {
var fnOld = ValidatorUpdateIsValid;
ValidatorUpdateIsValid = function() {
fnOld();
pi_CheckValidators();
}
}
};
If all you're doing is validating the input on three text fields, you can probably use the jQuery validator plugin with the built-in methods for date fields. Just override the showErrors handler to display the first error in the array in the callback args.
I'm not familiar with Page_Validators. Is that a .net thing?
精彩评论