使用JQuery验证插件

时间:2012-05-09 08:02:06

标签: jquery jquery-plugins

[增订]

我正在使用JQuery Validate插件来验证一个简单的表单。 它的形式是HTML:

<form id="commentForm">
<div id="ContactUsDIV">
    <div id="right">
        <div class="ContactFormInput">Subject:</div>
        <input id="ContactFormSubject" name="ContactFormSubject" type="text" class="input"/>
        <label for="ContactFormSubject" class="validationMessage"></label>

        <div class="ContactFormInput">Full Name:</div>
        <input id="FullName" name="FullName" type="text" class="input"/>
        <label for="FullName" class="validationMessage"></label>

        <div class="ContactFormInput">EMail:</div>
        <input id="EmailAddress" name="EmailAddress" type="text" class="inputLTR"/>
        <label for="EmailAddress" class="validationMessage"></label>

        <div>
            <input id="SendMessage" type="submit" value="Send" class="ContactFormSubmit"/>
        </div>
    </div>
    <div id="left">
        <div class="ContactFormInput">Message:</div>
        <textarea id="UserComment" cols="20" rows="2"></textarea>
        <label for="UserComment" class="validationMessage"></label>
    </div>

</div>
</form>

Java脚本:

<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function () { alert("submitted!"); }
    });

    $().ready(function () {

        // validate signup form on keyup and submit
        $("#commentForm").validate({
            rules: {
                ContactFormSubject: "required",
                FullName: "required",
                username: "required",
                EmailAddress: {
                    required: true,
                    email: true
                },
                UserComment: "required"
            },
            messages: {
                ContactFormSubject: "blah blah",
                FullName: "blah blah",
                username: "blah blah",
                email: "blah blah",
                UserComment:"blah blah"
            }
        });

    });
</script>

但是当我点击发送按钮时没有任何反应。

此处可用:http://tinkerbin.com/wRh09opb

3 个答案:

答案 0 :(得分:5)

尝试使用此方法获取正确的电子邮件验证消息

<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function () { alert("submitted!"); }
    });

    $(document).ready(function () {

        // validate signup form on keyup and submit
        $("#commentForm").validate({
            rules: {
                ContactFormSubject: "required",
                FullName: "required",
                username: "required",
                EmailAddress: {
                    required: true,
                    email: true
                },
                UserComment: "required"
            },
            messages: {
                ContactFormSubject: "blah blah",
                FullName: "blah blah",
                username: "blah blah",
                EmailAddress: {required:"blah blah", email:"blah blah"},
                UserComment:"blah blah"
            }
        });

    });
</script>

答案 1 :(得分:1)

取代ContactFormSubject:“required”

像这样使用

规则:

  ContactFormSubject: {
                required: true }

消息:

ContactFormSubject: { required : "Please fill the field" }

样品:

$("#AdditionalDetailsForm").validate({
    rules: {
        ignore: ":not(:visible)",
        Account_Number: {
            required: true
        },
        BaseJurisdiction: {
            required: true
        }
    },
    messages: {
        Account_Number: {
            required: "<br/> Please enter the field"
        },
        BaseJurisdiction: {
            required: "<br/> Please enter the field"
        }
    }
});

答案 2 :(得分:0)

尝试使用此Javascript:

<script type="text/javascript">
    $.validator.setDefaults({
        submitHandler: function () { alert("submitted!"); }
    });

    $.ready(function () {

        // validate signup form on keyup and submit
        $("#commentForm").validate({
            rules: {
                ContactFormSubject: "required",
                FullName: "required",
                username: "required",
                EmailAddress: {
                    required: true,
                    email: true
                },
                UserComment: "required"
            },
            messages: {
                ContactFormSubject: "blah blah",
                FullName: "blah blah",
                username: "blah blah",
                email: "blah blah",
                UserComment:"blah blah"
            }
        });

    });
</script>