gpt4 book ai didi

validation - 使用ajax进行自定义验证.....这里远程方法不起作用

转载 作者:行者123 更新时间:2023-12-01 01:15:33 25 4
gpt4 key购买 nike

我正在使用 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>

请帮忙。谢谢

最佳答案

用这个替换你的代码

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();
}
};

关于validation - 使用ajax进行自定义验证.....这里远程方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17963147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com