How do I tell jQuery Validation that the AJAX validation is good?
I'm trying to use the jQuery validation plugin on a for开发者_运维问答m. I've added an AJAX routine to validate one of the fields, the rest just need the regular validation methods.
Here's the code that I'm using.
$(document).ready(function(){
$.validator.addMethod("unique_rep", function(value, element){
$.ajax({
beforeSend: function(){
spinnerStart();
},
type: "POST",
url: "servertime3.php",
data: "unique_rep="+value,
dataType:"json",
success: function(msg){
spinnerStop(msg);
}
})
},"");
$("#register").validate({
rules: {
rep_id:{
unique_rep: true,
},
last_name:{
required: true,
minlength: 3
},
first_name:{
required: false,
minlength: 3
},
middle_initial:{
maxlength: 1,
},
email:{
required: false,
email: true
},
url:{
required: false,
url: true
}
}
})
});
spinnerStart() starts an in progress indicator next to the field.
spinnerStop() displays the result of the AJAX function which returns an array that contains 'ok' which is true or false to show if the function succeeded and 'text' which is details of what went wrong.
This works fine EXCEPT that it's not submitting. Where and how do I tell the validation plugin that the rep_id field is valid?
Thanks.
I'm not sure that i understood your question, also i'm sure that you know this but you also need to attach validate() plugin to a form..
$("#myform").validate({
submitHandler: function(form) {
form.submit();
alert("submited!");
}
});
By having properties like required: true
you're setting up constraints for the plugin to match against. The idea is for it to tell you if the form is valid or not.
Your question is a bit ambiguous.... I would narrow down the problem a bit m ore and you might get more answers... BUT here is me shooting the hip..
Try submitting the form manually:
$('form').submit(function(){
if($('form').valid()){
$('form').submit();
} else {
return false;
}
})
精彩评论