电话号码和电子邮件验证无法在使用asp .net的javascript中运行

时间:2015-10-26 06:43:38

标签: javascript asp.net

这是我的javascript验证码:

function Register() {
    if (document.getElementById("<%=Textusername.ClientID%>").value == "") {
        alert("Enter Your Name !");
        document.getElementById("<%=Textusername.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textpwd.ClientID%>").value == "") {
        alert("Enter Your password !");
        document.getElementById("<%=Textpwd.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textemail.ClientID%>").value == "") {
        alert("Enter Your email!");
        document.getElementById("<%=Textemail.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textphone.ClientID%>").value == "") {
        alert("Enter Your phone num!");
        document.getElementById("<%=Textphone.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textaddress.ClientID%>").value == "") {
        alert("Enter Your address !");
        document.getElementById("<%=Textaddress.ClientID%>").focus();
        return false;
    }
    if (document.getElementById("<%=Textqualification.ClientID%>").value == "") {
        alert("Enter Your qualification!");
        document.getElementById("<%=Textqualification.ClientID%>").focus();
        return false;
    }
    var emailpat = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    var emailid = document.getElementById("<%=Textemail.ClientID%>").value;
    var matchArray = emailid.match(emailpat);
    if (matcharray == null) {
        alert("Your Email ID seems incorrect. Please try again.");
        document.getElementById("<%=Textemail.ClientID%>").focus();
        return false;
    }
    var phonenumfilter = /^[0-9]+$/;
    var phonenum = document.getElementById("<%=Textphone.ClientID%>").value;
    var matchArray88 = phonenum.match(phonenumfilter);
    if (matchArray88 == null) {
        alert("Your Phone Number seems incorrect. Please try again.");
        document.getElementById("<%=Textphone.ClientID%>").focus();
        return false;
    }
}

我通过一些验证创建了注册表单,

当我输入无效的电子邮件或电话号码时,它不会显示警报。

但是当我离开时,所有字段都是空的,它会显示警告。

我可以知道,错误?

谢谢,

2 个答案:

答案 0 :(得分:0)

检查这是否有帮助。

电子邮件验证

function validateEmail(form_id,email) {

   var regEmailExp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(!regEmailExp.test(address)) {     
      alert('Invalid Email Address');
      return false;
   }
}

电话号码

function IsValidPhoneNumber(txtPhNumber) {

var ph = ^([0|\+[0-9]{1,5})?([7-9][0-9]{9})$;

var txtPhNo = document.getElementById(txtPhNumber);

if (ph.test(txtPhNo.value) == false) {

    alert("Please enter valid phone number.");

    txtPhNo.focus();

    return false;

}

return true;

}

答案 1 :(得分:0)

电话号码尝试此验证

    <asp:TextBox ID="txtmobileno" onpaste="return false;" runat="server" CssClass="form-control" MaxLength="10" OnClientClick="javascript:return Validate()" ></asp:TextBox>

    function Validate(){
    var phone = document.getElementById("<%=txtemail.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;
}

电子邮件

<asp:TextBox ID="txtemail" onpaste="return false;" MaxLength="50" runat="server" CssClass="form-control"></asp:TextBox>





  if (document.getElementById("<%=txtemail.ClientID%>").value != "")
         {
            var filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
        if (!filter.test(document.getElementById("<%=txtemail.ClientID%>").value)) 
      {
        alert('Please Provide A Valid Email Id');
       document.getElementById("<%=txtemail.ClientID%>").value="";
       document.getElementById("<%=txtemail.ClientID%>").focus();
       return false;
                            }
                        }