0

I am using the below code to validate the Email address and to compare the Email address with Re-type Email address. but when i type in the wrong format of email address and click Register, somehow i am not getting the pop up.

<%@ 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._%+-]+@[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>
Anuya
  • 8,082
  • 49
  • 137
  • 222
  • It might help if you told us what the "wrong format of email address" you typed in was - that way we might be able to tell what's wrong with your Regex. – Bridge Aug 10 '12 at 11:58

2 Answers2

0

try this

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

use above function according to you.

Ref: Validate email address in JavaScript?

Community
  • 1
  • 1
Jitendra Pancholi
  • 7,897
  • 12
  • 51
  • 84
0

Asp .net have Regular Expression validator and compare validator you should use them

here are links for them

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

Rnk Jangir
  • 711
  • 6
  • 23