使用Jquery进行表单验证

时间:2011-01-05 16:08:23

标签: jquery

当我提交表单时,我需要通过jquery验证txt字段是否为null和其他内容。但是对于所有其他按钮中只有一个提交按钮,它必须验证。我正在执行以下操作

$(document).ready( function(){
    /* validate form */
    $("#getEstimateId").click(function(e) {
        alert('get Estimate');
    });
    $("#get").click(function(e) {
        alert('get');
    });
    $("#getEstimateId").click(function() {
        alert('getting in ');
        var validator = $("#editDMBasketContentsId").validate({
            rules: {
                "postal": {
                    required:true,
                    number: true,
                    exactlength : 4,
                    digits : true
                }
            },
            messages: {
                "postal": {
                    required: "This field is required",
                    number :"Invalid entry please enter numbers only.",
                    exactlength : "Exactly 4 characters must be entered",
                    digits : "Please enter digits only"
                }
            },
            errorPlacement: function (error, element) {
                if (element.is(":radio"))
                    error.appendTo(element.parent().next().next());
                else if (element.is(":checkbox"))
                    error.appendTo(element.next().next());
                else
                    error.appendTo(element.parent().find(".status"));
            },
            submitHandler: function () {
                document.forms['deliveryEstimate'].submit();
            },
            success: function (label) {
                label.html(" ").addClass("checked");
            }
        });
    });
});

当我单击按钮并显示所有警报但是然后验证没有发生时,它会进入方法内部。关于它为何如此发生的任何建议

1 个答案:

答案 0 :(得分:0)

前几行阻止了验证现在将其整理出来

$(document).ready( function(){
    /* validate form */
    $("#getEstimateId").click(function() {
        alert('getting in ');
        var validator = $("#editDMBasketContentsId").validate({
            rules: {
                "postal": {
                    required:true,
                    number: true,
                    exactlength : 4,
                    digits : true
                }
            },
            messages: {
                "postal": {
                    required: "This field is required",
                    number :"Invalid entry please enter numbers only.",
                    exactlength : "Exactly 4 characters must be entered",
                    digits : "Please enter digits only"
                }
            },
            errorPlacement: function (error, element) {
                if (element.is(":radio"))
                    error.appendTo(element.parent().next().next());
                else if (element.is(":checkbox"))
                    error.appendTo(element.next().next());
                else
                    error.appendTo(element.parent().find(".status"));
            },
            submitHandler: function () {
                document.forms['deliveryEstimate'].submit();
            },
            success: function (label) {
                label.html(" ").addClass("checked");
            }
        });
    });
});