jQuery验证addmethod总是返回false [英] jQuery validation addmethod always return false
问题描述
我使用jQuery 1.7.2和jQuery 1.8.2验证我添加了Ajax,我检查响应正确的值也传递正确的,但仍然无法得到验证。这是我的code
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
VAR验证= $(#Form1的)。验证({
规则:{
ctl00 $ $的ContentPlaceHolder txCivilID:{
要求:真实,
rangelength:[12,12],
数字:真实,
checkCivilID:真
},
ctl00 $ $的ContentPlaceHolder txFirstName:{
要求:真
},
ctl00 $ $的ContentPlaceHolder txLastName:{
要求:真
},
ctl00 $ $的ContentPlaceHolder txMobile:{
要求:真实,
rangelength:[8,8]
数字:真
},
ctl00 $ $的ContentPlaceHolder dpUserType:{
要求:真
}
},
消息:{
ctl00 $ $的ContentPlaceHolder txCivilID:{
要求:对公民需要!
rangelength:民间的应该是{0}长
数字:民间的应该只是数字!
},
ctl00 $ $的ContentPlaceHolder txFirstName:名是必需的!
ctl00 $ $的ContentPlaceHolder txLastName:姓氏是必需的!
ctl00 $ $的ContentPlaceHolder txMobile:{
要求:手机号码是必需的!
rangelength:手机号码应该是长度{0}!
数字:手机号码应该只是数字!
},
ctl00 $ $的ContentPlaceHolder dpUserType:选择用户类型!
},
errorPlacement:功能(错误,元素){
$([+ element.attr(ID)+'为='标签')文本(error.text());
}
});
jQuery.validator.addMethod(checkCivilID功能(价值元素){
VAR响应='';
$阿贾克斯({
网址:CivilID.aspx / IsAlreadyAvailable
键入:POST,
异步:假的,
数据:{'civilID':'+价值+'},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:功能(数据){
返回data.d == 0?真假;
}
});
},CivilID已经存在);
});
< / SCRIPT>
我我addMethod改为远程方法,现在我的code是按以下,但仍然是相同的:(
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
VAR验证= $(#Form1的)。验证({
规则:{
ctl00 $ $的ContentPlaceHolder txCivilID:{
要求:真实,
rangelength:[12,12],
数字:真实,
遥控器:函数(){
VAR值= $('#<%= txCivilID.ClientID%GT;')。VAL();
变种R = {
的contentType:应用/ JSON的;字符集= UTF-8,
数据:JSON.stringify({civilID:值})
键入:POST,
数据类型:JSON
网址:CivilID.aspx / IsAlreadyAvailable
//异步:假的,
dataFilter:功能(数据){$返回.parseJSON(数据).D}
};
返回ř;
}
},
ctl00 $ $的ContentPlaceHolder txFirstName:{
要求:真
},
ctl00 $ $的ContentPlaceHolder txLastName:{
要求:真
},
ctl00 $ $的ContentPlaceHolder txMobile:{
要求:真实,
rangelength:[8,8]
数字:真
},
ctl00 $ $的ContentPlaceHolder dpUserType:{
要求:真
}
},
消息:{
ctl00 $ $的ContentPlaceHolder txCivilID:{
要求:对公民需要!
rangelength:民间的应该是{0}长
数字:民间的应该只是数字!
遥控器:CivilID已经存在
},
ctl00 $ $的ContentPlaceHolder txFirstName:名是必需的!
ctl00 $ $的ContentPlaceHolder txLastName:姓氏是必需的!
ctl00 $ $的ContentPlaceHolder txMobile:{
要求:手机号码是必需的!
rangelength:手机号码应该是长度{0}!
数字:手机号码应该只是数字!
},
ctl00 $ $的ContentPlaceHolder dpUserType:选择用户类型!
},
errorPlacement:功能(错误,元素){
$([+ element.attr(ID)+'为='标签')文本(error.text());
}
}); /*jQuery.validator.addMethod(\"checkCivilID功能(价值元素){
VAR响应='';
$阿贾克斯({
网址:CivilID.aspx / IsAlreadyAvailable
键入:POST,
异步:真实,
数据:{'civilID':'+价值+'},
的contentType:应用/ JSON的;字符集= UTF-8,
数据类型:JSON
成功:功能(数据){
返回data.d == 0?真假;
}
});
},CivilID已经存在); * /
}); < / SCRIPT>
我解决了这个问题,因为人们告诉我,使用远程方法,我这样做,但它仍然是给同样的问题。然后,我开始改变了Web服务的价值。这是第一个布尔然后我改变布尔,但仍然是同样的问题,现在我让它字符串和返回值true和false。
宾果它开始工作:D
I am using jQuery 1.7.2 and jQuery validation 1.8.2 I added ajax which I check response all correct value is also passing correct but still I am not getting validation. This is my code
<script type="text/javascript">
$(document).ready(function () {
var validator = $("#form1").validate({
rules: {
ctl00$ContentPlaceHolder$txCivilID: {
required: true,
rangelength: [12,12],
digits: true,
checkCivilID: true
},
ctl00$ContentPlaceHolder$txFirstName: {
required: true
},
ctl00$ContentPlaceHolder$txLastName: {
required: true
},
ctl00$ContentPlaceHolder$txMobile: {
required: true,
rangelength: [8,8],
digits: true
},
ctl00$ContentPlaceHolder$dpUserType: {
required: true
}
},
messages: {
ctl00$ContentPlaceHolder$txCivilID: {
required: "Civil for is required!",
rangelength: "Civil for should be {0} in length!",
digits: "Civil for should be only numbers!"
},
ctl00$ContentPlaceHolder$txFirstName: "First Name is required!",
ctl00$ContentPlaceHolder$txLastName: "Last Name is required!",
ctl00$ContentPlaceHolder$txMobile: {
required: "Mobile number is required!",
rangelength: "Mobile number should be {0} in length!",
digits: "Mobile number should be only numbers!"
},
ctl00$ContentPlaceHolder$dpUserType: "Select User Type !"
},
errorPlacement: function (error, element) {
$('label[for="' + element.attr("id") + '"]').text(error.text());
}
});
jQuery.validator.addMethod("checkCivilID", function (value, element) {
var response = '';
$.ajax({
url: "CivilID.aspx/IsAlreadyAvailable",
type: "POST",
async: false,
data: "{'civilID':'" + value + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
return data.d == 0 ? true : false;
}
});
}, "CivilID is Already Exists");
});
</script>
I change my addMethod to remote method and now my code is as per below but still it is same :(
<script type="text/javascript">
$(document).ready(function () {
var validator = $("#form1").validate({
rules: {
ctl00$ContentPlaceHolder$txCivilID: {
required: true,
rangelength: [12,12],
digits: true,
remote: function () {
var value = $('#<%=txCivilID.ClientID%>').val();
var r = {
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ civilID: value }),
type: "POST",
dataType: "json",
url: "CivilID.aspx/IsAlreadyAvailable",
//async: false,
dataFilter: function (data) { return $.parseJSON(data).d }
};
return r;
}
},
ctl00$ContentPlaceHolder$txFirstName: {
required: true
},
ctl00$ContentPlaceHolder$txLastName: {
required: true
},
ctl00$ContentPlaceHolder$txMobile: {
required: true,
rangelength: [8,8],
digits: true
},
ctl00$ContentPlaceHolder$dpUserType: {
required: true
}
},
messages: {
ctl00$ContentPlaceHolder$txCivilID: {
required: "Civil for is required!",
rangelength: "Civil for should be {0} in length!",
digits: "Civil for should be only numbers!",
remote:"CivilID is Already Exists"
},
ctl00$ContentPlaceHolder$txFirstName: "First Name is required!",
ctl00$ContentPlaceHolder$txLastName: "Last Name is required!",
ctl00$ContentPlaceHolder$txMobile: {
required: "Mobile number is required!",
rangelength: "Mobile number should be {0} in length!",
digits: "Mobile number should be only numbers!"
},
ctl00$ContentPlaceHolder$dpUserType: "Select User Type !"
},
errorPlacement: function (error, element) {
$('label[for="' + element.attr("id") + '"]').text(error.text());
}
});
/*jQuery.validator.addMethod("checkCivilID", function (value, element) {
var response = '';
$.ajax({
url: "CivilID.aspx/IsAlreadyAvailable",
type: "POST",
async: true,
data: "{'civilID':'" + value + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
return data.d == 0 ? true : false;
}
});
}, "CivilID is Already Exists");*/
});
</script>
I solved the problem, as people told me use remote method and I did that but still it was giving same issue. Then I start changing out value of web service. It was first bool then I change to Boolean but still it was same issue and now I make it string and returning value true and false.
Bingo it start working :D
这篇关于jQuery验证addmethod总是返回false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!