Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
am new in javascript.
i have used to develop one asp.net application.
here i have make it used one validation process using java script.
i need to validate my email address.
if my email address is valid, i have redirect to another page.
if my email address is not valid, am getting invalid mail id.
but my problem is,
if my email address is valid, i can't able to redirect to another page. please find the code below
and let me know where am made mistake or else give me a valid code.

my code:

XML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">
 function validEmail()
{

var mail = document.getElementById('email').value; // where `mail` is id of your input form
var email = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;

  if (mail.match(email))
  {

    window.location="Default3.aspx";
  }
  else
  {
      alert("Invalid Email !!!");

        return false;
  }

   return true;
}
</script>
</head>
<body>
    <form id="form1" name="form1" action="" method="post" runat="server">
    <div>
    <input type="text" name="email" id="email">

        <asp:Button ID="Button1" runat="server" OnClientClick="return validEmail()" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>



thanks to advance.
Posted
Comments
Ankur\m/ 19-Mar-14 7:40am    
Debug the JavaScript and see what's wrong.

Add "return false" when the validation passes, so as to cancel the button1_click event.
window.location="Default3.aspx";
return false;
 
Share this answer
 
v3
change as per below line and see whether its working or not

C#
if (mail.match(email))
{
   this.submit();
}
 
Share this answer
 
Comments
stellus 19-Mar-14 7:40am    
hi syed, thanks for your reply,
its not working at all
Kornfeld Eliyahu Peter 19-Mar-14 7:44am    
His form action is empty...
stellus 19-Mar-14 7:53am    
instead of form action i just make form redirect to java script
Try this:
JavaScript
window.location.href = './Default3.aspx';
 
Share this answer
 
v2
Try this
C#
if (email.test(mail))
{
    window.location.href = 'URL to redirect';
}
else
{
    alert("Enter a valid email");
    this.focus();
}

or
 if (email.test(mail.value))
{
      window.location = 'Default.aspx';
}
else
{
     alert("Enter a valid email");
     this.focus();
}


Good Luck
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900