Friday, November 21, 2014

Email id validation using javascript in Asp.net

Introduction

In this post I will explain how to do email id validation in Asp.net javascript. My previous post SQL Query to find Nth Highest or Lowest salary of an employee table. How to send email in Asp.net c#

Explanation

Validation is a main purpose of a website for avoiding the invalid user entries. We can validate our controls in client side and server side. client side validation will be very simple and very fast.
How we are validate an email id here. we just check whether the email id must contain a some characters followed by '@' symbol and some character for domain name followed by a '.' (dot) symbol followed by some character.
eg : help.dotnetpgm@gmail.com is a valid email id.

here before '@' symbol we can allow a (.) dot symbol and (_) underscore.
Coding


We will use a textbox control for enter the email id.

Design part

<table>
 <tbody>
<tr>
 <td><b>Email ID :</b></td>
 <td><asp:textbox id="txtEmailID" runat="server">
 </asp:textbox></td>
 <td><asp:button id="btnSubmit" onclick="btnSubmit_Click" onclientclick="return EmailValidation(emailID);" runat="server">
 </asp:button></td>
 </tr>
</tbody></table>.

add the javascript in body tag
<script type="text/javascript">
 function EmailValidation(emailID)
 {
  
  // strong regular expression code for email id validation after dot only 2-4 characters only
  var regxEmailID =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
  if (regxEmailID.test(emailID)){
  return true; }
  else{
    alert('Please enter a valid eMailID!');
  return false;
  }
 } 
 </script>


Regular expression is a simple and easy to use. we can avoid multiple condition checking using regular expression.
Explanation of regular expression

([a-zA-Z0-9_\.\-]) -- means it will allow small alphabets, capital alphabets, numbers, dot symbol and underscore symbol.

\@(([a-zA-Z0-9\-])+\.) -- means it will allow @ symbol followed by small alphabets, capital alphabets numbers only and at last dot symbol

([a-zA-Z0-9]{2,4}) -- means it will allow small alphabets, capital alphabets numbers only and it should not more than four characters and less than one character
Note : Using the above client side javascript we can check the email id format only

My previous post SQL Query to find Nth Highest or Lowest salary of an employee table

4 comments:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai Dot Net Training in Chennai


    Dot Net Online Training Dot Net Online Training LINQ Online Training LINQ Online Training ASP.NET Online Training ASP.NET Online Training

    ReplyDelete
  2. According to the Certified Fraud Examiners, most fraud perpetrators do not have a criminal record because they are first offenders.
    http://foxempiretv.com/wp-content/plugins/zingiri-forum/mybb/showthread.php?tid=367687

    ReplyDelete

Followers