电子邮件地址验证与JavaScript不工作在ASP.net

问题描述:

我使用下面的代码来验证电子邮件地址,并将电子邮件地址与重新输入电子邮件地址进行比较。但是当我输入错误的电子邮件地址格式并点击注册时,不知何故,我没有得到弹出窗口。电子邮件地址验证与JavaScript不工作在ASP.net

<%@ Page Title="" Language="VB" MasterPageFile="~/Main.master" AutoEventWireup="false" 
    CodeFile="Registration.aspx.vb" Inherits="Registration" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server"> 
<script type="text/javascript" language="javascript"> 
    function verifyEmail() { 

     var status = false; 

     var emailRegEx = /^[A-Z0-9._%+-][email protected][A-Z0-9.-]+\.[A-Z]{2,4}$/i; 
     if (document.aspnetForm.txtEmailAddress.value.search(emailRegEx) == -1) 
     { 
      alert("Please enter a valid email address."); 
     } 
     else if (document.aspnetForm.txtEmailAddress.value != document.aspnetForm.txtVerifyEmailAddress.value) 
     { 
      alert("Email addresses do not match. Please retype them to make sure they are the same."); 
     } 
     else 
     { 
      alert("Woohoo! The email address is in the correct format and they are the same."); 
      status = true; 
     } 
     alert(status) 
     return status; 
    } 
</script> 

</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server"> 
    <table > 

     <tr> 
      <td > 
       <asp:Label ID="Label3" runat="server" Text="Email Address"></asp:Label> 
      </td> 
      <td > 
       <asp:TextBox ID="txtEmailAddress" Width="234px" runat="server"></asp:TextBox> 
      </td> 
      <td > 
       </td> 
     </tr> 
     <tr> 
      <td > 
       <asp:Label ID="Label4" runat="server" Text="Re-Enter Email"></asp:Label> 
      </td> 
      <td > 
       <asp:TextBox ID="txtVerifyEmailAddress" Width="234px" runat="server"></asp:TextBox> 
      </td> 
      <td > 
       </td> 
     </tr> 
     <tr> 
      <td > 

      </td> 
      <td > 
       <asp:Button ID="btnRegister" runat="server" Text="Register" OnClientClick ="verifyEmail();" value="Check Email Address"/> 
       &nbsp;&nbsp;&nbsp;&nbsp; 
       <asp:Button ID="btnCancel" runat="server" Text="Cancel" /> 
      </td> 
      <td > 
       </td> 
     </tr> 
    </table> 
</asp:Content> 
+0

可能有帮助,如果你告诉我们你输入的“错误格式的电子邮件地址”是什么 - 这样我们就可以知道你的正则表达式有什么问题。 – Bridge 2012-08-10 11:58:05

尝试根据你这个

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\ 
".+\"))@((\[[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,}))$/; 
    return re.test(email); 
} 

使用上述功能。

编号:Validate email address in JavaScript?

ASP.NET的具有正则表达式验证和比较验证,你应该使用它们

这里有他们的联系

http://asp-net-example.blogspot.in/2009/02/aspnet-regularexpressionvalidator.htmlhttp://asp.net-tutorials.com/validation/compare-validator/