Validate fields using JQuery validation without any submit?

前端 未结 5 2027
终归单人心
终归单人心 2020-11-29 02:39

I have a HTML5 web application with numerous user-entered fields, and I would like to do some client-side validation on those fields in javascript before sending them to the

5条回答
  •  无人及你
    2020-11-29 03:02

    Firstly, and most importantly, you must wrap your input elements inside

    tags for the jQuery Validate plugin to operate. However, a submit button is not required.

    Secondly, you can programatically trigger the validity test of any or all elements without a submit button by using the .valid() method.

    $(document).ready(function() {
    
        $('#myform').validate({  // initialize the plugin on your form.
            // rules, options, and/or callback functions
        });
    
        // trigger validity test of any element using the .valid() method.
        $('#myelement').valid();
    
        // trigger validity test of the entire form using the .valid() method.
        $('#myform').valid();
    
        // the .valid() method also returns a boolean...
        if ($('#myform').valid()) {
            // something to do if form is valid
        }
    
    });
    

    DEMO: http://jsfiddle.net/URQGG/

提交回复
热议问题