Jquery验证一般错误?

时间:2011-11-15 19:24:53

标签: jquery validation jquery-plugins

我希望能够检查表单的有效性。如果有任何错误,我想显示一般错误div,其中包含查看表单的说明。任何人都可以帮我解决语法问题吗?

2 个答案:

答案 0 :(得分:0)

$(function(){
    $('form').submit(function(event){ //when form is submitted
        var errors = false; //errors start as false
        if($('.email').val() === ''){ //this is where you can validate fields
            errors = true; //if validation fails errors = true
        }
        if(errors){ //if it does not pass validation
            event.preventDefault(); //don't submit form
            $('errorDiv').show(); //show the hidden div that tells the user there are errors
        }
    });
});

答案 1 :(得分:0)

这似乎是一个非常好的起点:

$(document).ready(function() {
    var submitted = false;
    ('.selector').validate({
        showErrors: function(errorMap, errorList) {
            if (submitted) {
                var summary = "You have the following errors: \n";
                $.each(errorList, function() { summary += " * " + this.message + "\n"; });
                alert(summary);
                submitted = false;
            }
            this.defaultShowErrors();
        },          
        invalidHandler: function(form, validator) {
            submitted = true;
        }
    });
});

由David Fullerton提供:{{3p>