victorjonsson / jQuery-Form-Validator

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

Trigger validation from outside #65

Closed jaanush closed 10 years ago

jaanush commented 10 years ago

It would be nice if there was some way to trigger validation without submitting the form, like $('#myform').isValid()

jaanush commented 10 years ago

This would be a great addition since there seems to be a general lack of jQuery validation solutions that do not rely on submitting the form - something you don't want in a lot of ajax solutions

carlos-granadeno commented 10 years ago

but, this jQuery validation works indeed with the OnBlur event...the user knows all the invalid inputs when the control lost focus!!! you don't need submit anything. I use it with my xajax project and combined with a good CSS works like a charm. this is a basic example: image looks like this: image

As you can saw, the example don't have any submit button. I deleted

victorjonsson commented 10 years ago

Have I understood this correctly that you want $(...).isValid() to do a silent validation of the form and return a boolean whether the form is valid (without triggering any error messages)?

Like Carlos points out you don't need to have a submit button.

<html>
<head>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/jquery-form-validator/2.1.27/jquery.form-validator.min.js"></script>
</head>
<body>
  <form action="#" id="the-form">
     <input type="text" name="user" data-validation="required" />
  </form>

  <script>

   // setup validation on form
   $.validate();

   // Ajax action when form is submitted
   var $form = $('#the-form').on('submit', function() {
      // ajax stuff...
      return false;
   });

   // programmatically submitting the form
   $form.get(0).submit(); // or $form.trigger('submit')
  </script>

</body>
</html>
kirbs- commented 10 years ago

Can't you use $.validateForm(language, config) for this?

victorjonsson commented 10 years ago

Yes, but then you must create the language and config variable yourself.

tyresswhitejr commented 10 years ago

Use the triggerHandler to trigger a submit without actually submitting the form.

$('#some-button-id').on('click', function(evt){
    evt.preventDefault();
    $(this).closest('form').triggerHandler('submit');
});

Then you can call your after validation function in the onSuccess callback.

$.validate({
     form : '#someFormId',
     onSuccess : function(){
          //Do Work Here
     }
});
victorjonsson commented 10 years ago

I'm closing this for now...

themhz commented 10 years ago

great ! thank you