Javascript Mobilenum验证在aspx中不起作用
我已在我的aspx页面中添加了手机号码验证 这是我的密码:Javascript Mobilenum验证在aspx中不起作用,javascript,asp.net,Javascript,Asp.net,我已在我的aspx页面中添加了手机号码验证 这是我的密码: <tr> <td> <asp:Label ID="label7" runat="server" Text="Mobile Number"></asp:Label> </td> <td> </td> <td> <asp:TextB
<tr>
<td>
<asp:Label ID="label7" runat="server" Text="Mobile Number"></asp:Label>
</td>
<td>
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" CssClass="form-control" MaxLength="10"
OnClientClick="return Validatemobilenum();"></asp:TextBox>
</td>
</tr>
脚本:
function Validatemobilenum() {
var phone = document.getElementById("<%=Textmobilenum.ClientID%>");
var RE = /^[\d\.\-]+$/;
if (phone.value != "") {
if (!RE.test(phone.value)) {
alert("You have entered an invalid phone number");
return false;
}
}
else {
alert("please Enter mobile number");
return false;
}
return true;
}
函数Validatemobilenum(){
var phone=document.getElementById(“”);
变量RE=/^[\d\.\-]+$/;
如果(phone.value!=“”){
如果(!重新测试(电话值)){
警报(“您输入的电话号码无效”);
返回false;
}
}
否则{
警报(“请输入手机号码”);
返回false;
}
返回true;
}
当我运行上面的代码时
即使我为手机号码插入字符,它也能工作。也就是说,上面的脚本不起作用
我可以知道吗,我错在哪里了
任何帮助都将不胜感激
谢谢,解决方案1 您需要在属性上编写代码,如下所示:-
<asp:TextBox ID="Textmobilenum" runat="server" CssClass="form-control" MaxLength="10" onblur="return Validatemobilenum()"></asp:TextBox>
还有你的js函数
function Validatemobilenum() {
var phone = document.getElementById("<%=Textmobilenum.ClientID%>");
var RE = /^[\d\.\-]+$/;
if (phone.value != "") {
if (!RE.test(phone.value)) {
alert("You have entered an invalid phone number");
return false;
}
alert('Correct mobile number entered');
}
else {
alert("please Enter mobile number");
return false;
}
return true;
}
函数Validatemobilenum(){
var phone=document.getElementById(“”);
变量RE=/^[\d\.\-]+$/;
如果(phone.value!=“”){
如果(!重新测试(电话值)){
警报(“您输入的电话号码无效”);
返回false;
}
警报(“输入的手机号码正确”);
}
否则{
警报(“请输入手机号码”);
返回false;
}
返回true;
}
注意:-不确定您的正则表达式这是因为您在OnClientClick中调用该方法,因此您需要onkeyup或onkeydown。请详细说明您的注释好吗?请详细说明您的注释好吗?谢谢
。正如我之前所说,在OnClientClick上添加一个onkeypress属性,在函数中,您需要跟踪键盘事件,并限制用户只输入数字而不输入其他内容。我不希望onkeyup和onkeydown。。我可以知道吗,有没有可能添加onclientclick?它不是按钮。。我已经创建了一个表单,它包含手机号码文本框。然后在更新之前检查我的第一个答案。它工作了。我只需要点击一下。。我曾经。。现在它不起作用了..谢谢你不能在textbox上添加onClientclick
,你需要一些点击事件来触发它,textbox永远不能这样做。无论如何,我已经为您提供了两个可行的解决方案。谢谢
function Validatemobilenum() {
var phone = document.getElementById("<%=Textmobilenum.ClientID%>");
var RE = /^[\d\.\-]+$/;
if (phone.value != "") {
if (!RE.test(phone.value)) {
alert("You have entered an invalid phone number");
return false;
}
alert('Correct mobile number entered');
}
else {
alert("please Enter mobile number");
return false;
}
return true;
}