Trigger parsley validation without submit form?

Hoan Dang picture Hoan Dang · Apr 1, 2014 · Viewed 40.7k times · Source

Given this code, it never works and always returns true whatsoever ?

<form id="my-form" data-validate="parsley">
  <p>
    <label for="username">Username * :</label>
    <input type="text" id="username" name="username" data-required="true" >
  </p>

  <p>
    <label for="email">Email Address * :</label>
    <input type="text" id="email" name="email" data-required="true" >
  </p>

  <br/>

  <!-- Validate all the form fields by clicking this button -->
  <a class="btn btn-danger" id="validate" >Validate All</a>
</form>

<script>
var $form = $('#my-form');
$('#validate').click (function () {
    if ( $form.parsley('validate') )
      console.log ( 'valid' ); <-- always goes here
    else
      console.log ('invalid');
});

</script>

So my question is if there is a way to trigger parsley validation without adding a submit button ?

Answer

guillaumepotier picture guillaumepotier · Apr 1, 2014

$form.parsley('validate') is 1.x API. It was deprecated in 2.x versions you might use.

Try $form.parsley().validate() instead.

Best