jquery validation with addMethod fails when it shouldn't -
i have idea may stupid, i've been stuck on hours.
i have simple form, jquery validation using ajax determine whether email address in database. works great. when email address found, reports duplicate, , when not, silent.
but when enter email know new, , fill out rest of form, , press submit, prevents form submission , sets focus on email field.
here's jquery:
$(document).ready(function(){ var email; var name; var passwd; var passwd_repeat; //form validation rules $("#regform1").validate({ rules: { name: { required: true, minlength: 3, }, passwd: { required: true, minlength: 8, }, passwd_repeat: { required: true, equalto: passwd, minlength: 8 } }, messages: { name: "please enter name.", passwd: "please enter password.", passwd_repeat: "passwords must match." } }); $.validator.addmethod("validateuseremail", function(value, element) { var inputelem = $('#regform1 :input[name="email"]'), data = { "email=" : inputelem.val() }, ereport = ''; //error report*/ var datastring = 'email='+ inputelem.val(); $.ajax( { type: "post", url: '/checkdups.php', data: datastring, success: function(html) { if (html !== 'true') { $('#email').after('<label class="error" for="email">error - '+inputelem.val()+' in use. please <a href="http://tester4.com/staff/auth/login">log in</a> '+inputelem.val()+'.</label>'); return false; } else { return true; } }, error: function(xhr, textstatus, errorthrown) { alert('ajax loading error... ... '+url + query); return false; } }); }, ''); $(':input[name="email"]').rules("add", { "validateuseremail" : true} );
});
i'm adding rule addmethod instead of using .remote because couldn't thing work more first time (meaning not reject succession of dupes without page refresh), , suggested workaround. when comment added method out, allows me submit form, of course not check dupes.
anyway, insights appreciated.
john
you should use remote
. i'm not sure why weren't getting work expected - remote does called multiple times. way works default not make call remote page until hit submit, , if remote call says bad, every keystroke after generate new remote call.
here simple working example can build off of. set return false until you've made 3 changes. should able follow these step:
- enter john@rand.com , click submit. note marked invalid.
- delete m (invalid)
- delete o (invalid)
- delete c (invalid due not being valid email address now)
- type
com
(fixed) - submit (works)
the key code fiddle in rules object (response global counter example):
rules: { email: { email: true, remote: { url: '/echo/json/', data: { json: function(){ response++; return (response > 3)?'true':'false'; } }, complete: function(data){ $('#log').append('remote triggered<br>'); }, type: 'post' }, required: true } }
Comments
Post a Comment