简体   繁体   中英

Preventing submission after jQuery Validation Engine

Once clicking button, wanting to prevent submission to local storage if any required fields are not filled out. Both functions work properly. Just trying to prevent LocalStorage.save() from hitting if $("#formID").validationEngine(); finds a required field not completed.

<form id="formID" name="myForm">    

 <input class="validate[required]" type="text" id="agree" name="agree"/>         
 <button type="submit" value="Save" id="Save" onclick="clicked();">Submit Survey</button>

</form>

<script type="text/javascript">   
  function clicked() {
        if (confirm('Are you sure you want to submit?')) {

           $("#formID").validationEngine();               
           my.LocalStorage.save();

        } else {
            return false;
        }
    }
 </script>  

From source code there is a function validate() which returns true/false after validating

so use it

if( $("#formID1").validationEngine('validate'))
{
my.LocalStorage.save();
}

check this http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

From their documentation at http://posabsolute.github.io/jQuery-Validation-Engine/ :

validate


Validates a form or field, displays error prompts accordingly.
Returns true if the form validates, false if it contains errors.

It is inversed for fields , it return false on validate and true on errors.

When using form validation with ajax, it returns undefined , the result is delivered asynchronously via function options.onAjaxFormComplete .

 // form validation alert( $("#formID1").validationEngine('validate') ); // field validation alert( $("#emailInput").validationEngine('validate') ); 

So that would change your code to:

function clicked (e) 
{
    if ( confirm('Are you sure you want to submit?') ) 
       if ( $("#formID").validationEngine('validate') )
           my.LocalStorage.save();

    e.preventDefault();  
}

Take note that I added e as a parameter that needs event passed in via:

<button type="submit" value="Save" id="Save" onclick="clicked(event);">
    Submit Survey
</button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM