victorjonsson / jQuery-Form-Validator

[DISCONTINUED] jQuery plugin that makes it easy to validate user input while keeping your HTML markup clean from javascript code.
972 stars 476 forks source link

validate() Doesn't work inside an on('click' function #614

Open aruberutochan opened 7 years ago

aruberutochan commented 7 years ago

Hi. Thank you very much for your plugin, it is amazing. I am having an issue while I am trying to validate some fields programatically. In essential: I have a form that ends with a validate button, I'd like to program a web form which checks some fields passed with an array of Ids like array = ['#myId1', '#myId2'] i have this code:

$('#validate-button').on('click', function () {
   for (var j = 0; j < array.length; j++) {
       var input_id = array[j];
       $(input_id).validate(function(valid, elem) {
          if (valid) {
              console.log('It looks valid');                                 
         } else {
              console.log('It is Invalid');
         }
    });  
});

However it is not sending anything to the console. Curiously if i put the funciton .validate(... outside of the button click, it works, but not when it is inside. Have I missed anything? Thanks

victorjonsson commented 7 years ago

Have you tried to debug every step?

$('#validate-button').on('click', function () {
   console.log('button clicked');
   for (var j = 0; j < array.length; j++) {
       var input_id = array[j];
      console.log($(input_id).length ? 'Found element to validate' : 'Did not find '+input_id);
       $(input_id).validate(function(valid, elem) {
          if (valid) {
              console.log('It looks valid');                                 
         } else {
              console.log('It is Invalid');
         }
    });  
});
aruberutochan commented 7 years ago

Yes, I put a console.log in every step and the one that doesn't fire is the one inside .validate function.

victorjonsson commented 7 years ago

Yes, we have a bug here http://jsbin.com/wiginireji/edit?html,console,output

victorjonsson commented 7 years ago

Fixed in tag 2.3.74

aruberutochan commented 6 years ago

sorry, I forgot to say thanks :-)

modelesque commented 6 years ago

Has this been added? I am using v.2.3.77 and validate() doesn't work for me. Similar code (running validate on click), put console.log everywhere and narrowed it down to the function not working. Any help?