检查表单验证是否为真

时间:2014-07-14 20:06:41

标签: javascript jquery jquery-validate

您好,我有一个表单,我想通过表单验证运行,然后提交。如何检查以下函数是否返回true以确认所有内容都已经过验证然后提交?

我创造了一个小提琴来测试

http://jsfiddle.net/WHGq2/

修改后的代码

     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
     <script type="text/javascript" src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script>

    <script type="text/javascript">
            $(function(){
        $("#form").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                        },
                phone: {
                    equired: true,
                    phone: true
                        }
                    },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
                phone: "Please provide a valid phone number.",
            }
        })  


    $('#form').submit(function(e){
        // Stop the form actually posting
        e.preventDefault();




        // I want to be able to do the check here if the form has passed validation
        if( $(this).valid() ) {
        // Stop the form actually posting

        // Send the request
        $.post('/submit.php', {
            name: $('#name').val(),
            email: $('#email').val(),
            phone: $('#phone').val(),
            message: $('#message').val()
        }, function(d){
            alert("Thank you for submitting your request someone will contact your shortly.");
        });
        }
    });
});
    </script>

4 个答案:

答案 0 :(得分:6)

您可以调用jquery验证的valid()方法。像

if($('#form').valid())

实际上,您可以在加载html页面时声明验证功能,并在提交时检查它是否有效

$(document).ready(function(){

$('#form').validate(rules...messages...);

如果是规则&amp;消息使用: -

  rules: {
            name: {
               required: true  
            },
            email: {
                required: true,
                email: true
                    },
            phone: {
                required: true,
                phone: true                       }
                },
        messages: {
            name: { required : "Please let us know who you are."},
           email: {required : "A valid email will help us get in touch with you.",email : "A valid email will help us get in touch with you."},
            phone: {required:"Please provide a valid phone number.",phone:"Please provide a valid phone number."}
        }

这是一个很好的做法,如果是电话号码,您需要拼写错误

$('#form').submit(function(evt) {
    evt.preventDefault();

    .....

    if( $('#form').valid() ) {
       //submit the form via ajax
    } else {
       //show errors
    }
 });

});

我认为你知道的比你还要添加每个输入字段的name属性: -

 <input type="text" name="email" />

答案 1 :(得分:2)

请注意,required规则中的 phone已写为equired,可能导致您的大多数问题。参见附带的演示。

$(function() {

    $('#form').validate(.......);

    $('#form').submit(function(e) {
        e.preventDefault();

        .....

        if( $(this).valid() ) {
           //submit the form via ajax or otherwise
        } else {
           //report errors
        }
    });

});

JSFIDDLE DEMO

答案 2 :(得分:0)

您可以使用插件的valid方法,如下所示。

$('#form').validate({...});

if ($('#form').valid()) {
    // do your thing
}

这是文档http://jqueryvalidation.org/valid

答案 3 :(得分:0)

更新了您的代码,添加了 submitHandler ,回调以在表单有效时处理实际提交。

    <script type="text/javascript">
            $(function(){
    $('#form').submit(function(e){

        $("#form").validate({
            debug: false,
            rules: {
                name: "required",
                email: {
                    required: true,
                    email: true
                        },
                phone: {
                    equired: true,
                    phone: true
                        }
                    },
            messages: {
                name: "Please let us know who you are.",
                email: "A valid email will help us get in touch with you.",
                phone: "Please provide a valid phone number.",
            },
            submitHandler: function() {  
                      // Stop the form actually posting
                       e.preventDefault();

                     // Send the request
                   $.post('/submit.php', {
                   name: $('#name').val(),
                   email: $('#email').val(),
                   phone: $('#phone').val(),
                   message: $('#message').val()
                   }, function(d){
                        alert("Thank you for submitting your request someone will contact your shortly.");
                   }); 
            }
    });
});
});
    </script>