Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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 : [email protected] 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


This post first appeared on Dotnet, please read the originial post: here

Share the post

Email id validation using javascript in Asp.net

×

Subscribe to Dotnet

Get updates delivered right to your inbox!

Thank you for your subscription

×