how to check if a form is valid programmatically using jQuery Validation Plugin

前端 未结 7 1801
余生分开走
余生分开走 2020-11-29 19:11

I have a form with a couple of buttons and I\'m using jQuery Validation Plugin from http://jquery.bassistance.de/validate/. I just want to know if there is any way I can che

相关标签:
7条回答
  • 2020-11-29 19:46

    valid() method.

    http://docs.jquery.com/Plugins/Validation/valid

    0 讨论(0)
  • 2020-11-29 19:47

    For Magento, you check validation of form by something like below.

    You can try this:

    require(["jquery"], function ($) {
        $(document).ready(function () {
            $('#my-button-name').click(function () { // The button type should be "button" and not submit
                if ($('#form-name').valid()) {
                    alert("Validation pass");
                    return false;
                }else{
                    alert("Validation failed");
                    return false;
                }
            });
        });
    });
    

    Hope this may help you!

    0 讨论(0)
  • 2020-11-29 19:54

    Use .valid() from the jQuery Validation plugin:

    $("#form_id").valid();
    

    Checks whether the selected form is valid or whether all selected elements are valid. validate() needs to be called on the form before checking it using this method.

    Where the form with id='form_id' is a form that has already had .validate() called on it.

    0 讨论(0)
  • 2020-11-29 19:55

    @mikemaccana answer is useful.

    And I also used https://github.com/ryanseddon/H5F. Found on http://microjs.com. It's some kind of polyfill and you can use it as follows (jQuery is used in example):

    if ( $('form')[0].checkValidity() ) {
        // the form is valid
    }
    
    0 讨论(0)
  • 2020-11-29 19:58

    For a group of inputs you can use an improved version based in @mikemaccana's answer

    $.fn.isValid = function(){
        var validate = true;
        this.each(function(){
            if(this.checkValidity()==false){
                validate = false;
            }
        });
    };
    

    now you can use this to verify if the form is valid:

    if(!$(".form-control").isValid){
        return;
    }
    

    You could use the same technique to get all the error messages:

    $.fn.getVelidationMessage = function(){
        var message = "";
        var name = "";
        this.each(function(){
            if(this.checkValidity()==false){
                name = ($( "label[for=" + this.id + "] ").html() || this.placeholder || this.name || this.id);
                message = message + name +":"+ (this.validationMessage || 'Invalid value.')+"\n<br>";
            }
        })
        return message;
    }
    
    0 讨论(0)
  • 2020-11-29 20:02

    iContribute: It's never too late for a right answer.

    var form = $("form#myForm");
    if($('form#myForm > :input[required]:visible').val() != ""){
      form.submit();
    }else{
      console.log("Required field missing.");
    }
    

    This way the basic HTML5 validation for 'required' fields takes place without interfering with the standard submit using the form's 'name' values.

    0 讨论(0)
提交回复
热议问题