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:

  1. enter john@rand.com , click submit. note marked invalid.
  2. delete m (invalid)
  3. delete o (invalid)
  4. delete c (invalid due not being valid email address now)
  5. type com (fixed)
  6. 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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -