在ASP.NET中使用Customvalidator出现问题
问题描述:
我正在尝试验证手机号码,如果手机号码大于10,则会给出错误或警告,这是我的代码:
i am trying to validate mobile number if mobile number is greater than 10 it give error or alert here is my code:
function check()
{
var pattern=/^[A-Za-z0-9]{10}$/;
var regexp = new RegExp(pattern);
//alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value);
if(regexp.test(document.getElementById("ctl00_ctl00_ContentPlaceHolder1_ContentProfile_txtMobileNumber").value))
{
return true;
}
else
{
alert('Phone is invalid');
return false;
}
}
并将此函数调用给自定义验证器
and calling this function to custom validator
<asp:CustomValidator ID="CVtxtPhoneNumber" ClientValidationFunction="check();" ControlToValidate="txtMobileNumber" runat="server" ErrorMessage="Enter a valid Phone No." ValidationGroup="EditProfile" SetFocusOnError="True" Display="Dynamic"></asp:CustomValidator>
问题是显示警报,但我只希望显示错误消息输入有效的电话号码".不是alert
The problem is that alert is displayed but i want the only the error message is displayed "Enter a valid phone no.". not the alert
答
/; var regexp = 新 RegExp(pattern); // alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value); 如果(regexp.test(document.getElementById(" ).返回 真; } 其他 { alert(' 电话无效'); 返回 假; } }
/; var regexp = new RegExp(pattern); //alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value); if(regexp.test(document.getElementById("ctl00_ctl00_ContentPlaceHolder1_ContentProfile_txtMobileNumber").value)) { return true; } else { alert('Phone is invalid'); return false; } }
并将此函数调用给自定义验证器
and calling this function to custom validator
<asp:CustomValidator ID="CVtxtPhoneNumber" ClientValidationFunction="check();" ControlToValidate="txtMobileNumber" runat="server" ErrorMessage="Enter a valid Phone No." ValidationGroup="EditProfile" SetFocusOnError="True" Display="Dynamic"></asp:CustomValidator>
问题是显示警报,但我只希望显示错误消息输入有效的电话号码".不是警报
The problem is that alert is displayed but i want the only the error message is displayed "Enter a valid phone no.". not the alert
首先,您需要具有两个参数sender和args
为check(sender,args)
,并且要显示错误消息时需要设置args.IsValid = false;
.验证者自己会注意休息.
您的检查功能将为
First you need to have two arguments sender and args
ascheck(sender,args)
and you need to setargs.IsValid = false;
when you want to show error message. Rest thing will be take care by validator itself.
your check function would be as
function check(sender,args)
{
var pattern=/^[A-Za-z0-9]{10}
/; var regexp = new RegExp(pattern); //alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value); if(regexp.test(document.getElementById("ctl00_ctl00_ContentPlaceHolder1_ContentProfile_txtMobileNumber").value)) { args.IsValid = true; } else { args.IsValid = false; } }
/; var regexp = new RegExp(pattern); //alert(document.getElementById("ctl00_ContentPlaceHolder1_txtPhoneNumber").value); if(regexp.test(document.getElementById("ctl00_ctl00_ContentPlaceHolder1_ContentProfile_txtMobileNumber").value)) { args.IsValid = true; } else { args.IsValid = false; } }