3

我正在使用 jquery.validate.js,在下面的代码中,远程和正则表达式分别工作,但是当我尝试将两者集成时,它就不起作用了。有人可以帮我在哪里做错了吗?

    <head>
<script type="text/javascript">


$(document).ready(function(){

        $("#clear").click(function(){

            $("input[type=text], textarea").val("");

        });
});

function submitForm() {

    $.validator.addMethod("subTitleVal", function(value, element) {
        return this.optional(element) || /^[A-Za-z\s\_,\.:;()''""]+$/.test(value); 
    }, "Enter Valid Name.");

    var validator = $("#company").validate({
        errorPlacement : function(error, element) {
            offset = element.offset();
            error.insertBefore(element)
            error.addClass('message'); 
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
        },
        rules : {
            name : {
                required : true,
                subTitleVal : true,
                      remote: {
                        type: 'POST',
                        url: "${pageContext.request.contextPath}/company/getDuplicate",
                        data: {"name":name},
                        dataType : "json",
                        success:function(data){
                            /* response = ( data == true ) ? true : false; */
                            if (data.name == true)
                            {
                                message: {
                                    name: 'The username is already in use!'
                                }
                            }
                        }
                    },
                }
                },
        },
        errorElement : "span",
        wrapper : "span",
        messages : {
            name : {
                required : "Name Is Required",
            }
        }
    });
    if(validator.form()){ 
        $('form#company').submit(); 
        }
};

</script>

   </head>
   <body>
 <form:form commandname="company" action="${pageContext.request.contextPath}/company/create.action"  method="post" modelAttribute="company" name="theform">

    <label>Name:</label>
    <form:input path="name" id="name"></form:input>

    <a href="Javascript:submitForm()">Add</a>

</form:form>

</body> 

请帮忙。谢谢

4

3 回答 3

2

用这个替换你的代码

function submitForm() {

    $.validator.addMethod("subTitleVal", function(value, element) {
                return this.optional(element)
                        || /^[A-Za-z\s\_,\.:;()''""]+$/.test(value);
            }, "Enter Valid Name.");

    var validator = $("#company").validate({
        errorPlacement : function(error, element) {
            offset = element.offset();
            error.insertBefore(element)
            error.addClass('message');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
        },
        rules : {
            name : {
                required : true,
                subTitleVal : true,
                remote : {
                    type : 'POST',
                    url : "${pageContext.request.contextPath}/company/getDuplicate",
                    data : {
                        name: function() { return $("#name").val(); }

                    }
                }
            }
        },
        errorElement : "span",
        wrapper : "span",
        messages : {
            name : {
                required : "Name Is Required",
                remote : "Name Already Taken."
            }
        }
    });
    if (validator.form()) {
        $('form#company').submit();
    }
};
于 2013-07-31T07:26:18.250 回答
1

文档

响应被评估为 JSON 并且对于有效元素必须为 true,并且对于无效元素可以是任何 false、未定义或 null,使用默认消息;或字符串,例如。“该名称已被占用,请尝试 peter123”以显示为错误消息。

所以你的验证 url 必须返回一个适当的值

尝试

function submitForm() {

    $.validator.addMethod("subTitleVal", function(value, element) {
                return this.optional(element)
                        || /^[A-Za-z\s\_,\.:;()''""]+$/.test(value);
            }, "Enter Valid Name.");

    var validator = $("#company").validate({
        errorPlacement : function(error, element) {
            offset = element.offset();
            error.insertBefore(element)
            error.addClass('message');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
        },
        rules : {
            name : {
                required : true,
                subTitleVal : true,
                remote : {
                    type : 'POST',
                    url : "${pageContext.request.contextPath}/company/getDuplicate",
                    data : {
                        "name" : name
                    }
                }
            }
        },
        errorElement : "span",
        wrapper : "span",
        messages : {
            name : {
                required : "Name Is Required"
            }
        }
    });
    if (validator.form()) {
        $('form#company').submit();
    }
};
于 2013-07-31T06:35:27.630 回答
0

您的括号未正确关闭。你有一个额外的},

保持代码缩进,以避免将来出现此类小错误。

这应该是您的代码:

function submitForm() {

    $.validator.addMethod("subTitleVal", function (value, element) {
        return this.optional(element) || /^[A-Za-z\s\_,\.:;()''""]+$/.test(value);
    }, "Enter Valid Name.");

    var validator = $("#company").validate({
        errorPlacement: function (error, element) {
            offset = element.offset();
            error.insertBefore(element)
            error.addClass('message');
            error.css('position', 'absolute');
            error.css('left', offset.left + element.outerWidth());
        },
        rules: {
            name: {
                required: true,
                subTitleVal: true,
                remote: {
                    type: 'POST',
                    url: "${pageContext.request.contextPath}/company/getDuplicate",
                    data: {
                        "name": name
                    },
                    dataType: "json",
                    success: function (data) {
                        /* response = ( data == true ) ? true : false; */
                        if (data.name == true) {
                            message: {
                                name: 'The username is already in use!'
                            }
                        }
                    }
                },
            }
        },
        errorElement: "span",
        wrapper: "span",
        messages: {
            name: {
                required: "Name Is Required",
            }
        }
    });
    if (validator.form()) {
        $('form#company').submit();
    }
};
于 2013-07-31T06:33:21.127 回答