Showing posts with label validate email. Show all posts
Showing posts with label validate email. Show all posts

Regex for email validation JavaScript

In this post we will see how to validate email format in Javascript using regular expression. We can use regular expression (/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i) to validate the email address in javascript.

Regular expression to validate email format in JavaScript

Example of email validation using regex in javascript

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Regex to validate email format in JavaScript</title>
  <script language="javascript" type="text/javascript">
    function Validate() {
      var emailRegex = new RegExp(/^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$/i);
      var emailAddress = document.getElementById("<%= txtemail.ClientID %>").value;
      var valid = emailRegex.test(emailAddress);
      if (!valid) {
        alert("Invalid e-mail address");
        return false;
      } else
        return true;
    }
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox runat="server" ID="txtemail"></asp:TextBox>
    <asp:Button runat="server" ID="Send" OnClientClick="return Validate();" Text="Send" />
  </div>
  </form>
</body>
</html>

Validate email format - RegularExpressionValidator

In asp.net in order to validate email address format we can use RegularExpressionValidator. We will see how to use RegularExpressionValidator to validate the email address format.For that we need to specify the Regular expression or regex in ValidationExpression property of the RegularExpressionValidator. Also we need to specify few other properties like ErrorMessage to display error message when email address is invalid. also we need to specify ControlToValidate property where we have to give textbox controls name in which user enters email address.

Validate email address using regular expression validator

Example to validate email address using RegularExpressionValidator

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>Validate email format </title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:TextBox ID="txtEmail" runat="server" />
    <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
    <asp:RegularExpressionValidator ID="validateEmail" runat="server" ErrorMessage="Invalid email."
      ControlToValidate="txtEmail" ValidationExpression="^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$" />
  </div>
  </form>
</body>


Also we need to check !Page.IsValid in event hadler of control like button or link...etc like
  protected void btnSubmit_Click(object sender, EventArgs e)
    {
      if (!Page.IsValid)
        return;
    }

c# regex for email address

In c# while dealing with email we need to validate email address. In order to validate email address we just check whether the email address format is correct or not. In C# we can use System.Text.RegularExpressions namesapce to match the email address for validation. In Regular Expressions namespace we have Regex class and from which .Match() method can be used to match the email address with the regular express defined using Regex class.

c# regex for email address

C# code to validate email format.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>C# validate email format regex</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtemail"></asp:TextBox>
        <asp:Button runat="server" ID="Send" OnClick="Send_Click" Text="Send" />
    </div>
    </form>
</body>
</html>

using System;
using System.Text.RegularExpressions;
public partial class Csharpregularexpressionemail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Send_Click(object sender, EventArgs e)
        {
            ValidateEmail();
        }
        private void ValidateEmail()
        {
            string email = txtemail.Text;
            Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$");
            Match match = regex.Match(email);
            if (match.Success)
                Response.Write(email + " is corrct");
            else
                Response.Write(email + " is incorrct");
        }
    }


In above example we have specified format for email regex using constructure call of Regex class. Then we have used .Match method to match the regular express pattern for email to match. And after that we have checked if match is successfull or not.