问题
I'm implementing the same validation in this link
http://alittlecode.com/files/jQuery-Validate-Demo/
So I display check marks next to elements on success, is there a way to display the checkmarks on form load when it has valid input by default, and display errors only on submit ?
here is the code
$("#frmsubmit").validate({
rules: {
citstyle: "required",
spelling: "required",
uploadfile: {required: function (element) {
if($("#filesent").is(':checked'))
{
return false;
}
else
{
return true;
}
} ,
filesize: 8388608},
wordsno: "required",
cellphone: "required",
country: "required",
address: "required",
paytype: "required"
},
highlight: function(label) {
$(label).closest('.control-group').addClass('error');
},
success: function(label) {
label
.text('OK!').addClass('valid')
.closest('.control-group').addClass('success');
}
});
Thanks
回答1:
Here's an working example: http://jsfiddle.net/Gajotres/TthG9/. My example was created from a code example taken from your link.
Code:
$(document).ready(function(){
$('#contact-form').validate(
{
rules: {
name: {
minlength: 2,
required: true
},
email: {
required: true,
email: true
},
subject: {
minlength: 2,
required: true
},
message: {
minlength: 2,
required: true
}
},
highlight: function(label) {
$(label).parent().find('.valid').each(function(){
$('label[class^="valid"]').remove();
});
},
success: function(label) {
if(label.text('OK!').parent().find('.valid').html() == null) {
label.removeClass('error').addClass('valid');
} else {
label.remove();
}
}
});
$("#contact-form").validate().form();
$('label[class^="error"]:not(.valid)').remove();
})
Method .form() can be used to trigger a form validation programmatically
来源:https://stackoverflow.com/questions/14001028/validate-form-on-load