Friday, April 17, 2009

Cross-browser javascript & asp.net(C#) email address validation




Please visit my new Web Site WWW.Codedisplay.com



Email address validation is a most common scenario for a developer. It will be the best if we can use javascript validation along with server side validation since user may off javascript from browser settings. The following example shows how you can validate an email address for a form. The script is cross browser compatible (works for all browsers such as IE, Firefox, Opera).

Javascript email validation using regular expression:
Here i tried to find out a simple solution to prhibit users to enter wrong email address. There is a lot of scope here to modify the regular expression. To do that you have to learn regular expression syntax. Now add the below function within your head tag or within the content tag for child page.

<script language="javascript" type="text/javascript">
function validateEmail()
{
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
return regex.test(document.getElementById('<%= txtEmail.ClientID %>').value);
}
</script>


So simple. Ok now we want to modify a little bit for better look & feel.

<script type="text/javascript">
function validateEmail()
{
var obj=document.getElementById('<%= txtEmail.ClientID %>');
var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
if(regex.test(obj.value))
{
//You can also assign stylesheet by
//obj.className='....';
obj.style.backgroundColor = '';
obj.style.backgroundColor = '';
return true;
}
else
{
//Changing Background Color so that user can understand that its invalid
//You can also assign stylesheet by
//obj.className='....';
obj.style.backgroundColor = '#FD5E53';
obj.style.borderColor = '#CD4A4A';
return false;
}
}
</script>


Now i am showing how we use the above javascript email validation function in our aspx page.

<asp:TextBox ID='txtEmail' runat="server"></asp:TextBox>
<asp:Button id="cmdSave" runat="server" Text="Save" OnClientClick="return validateEmail();" />


Simple output:


Asp.net C# email address validation:
I have already explained why we need to write server side validation. Now i want to show you an example how we can validate email address:

For code reusability it will be best to add a static class in our project for such type of validation. Here i am adding a static class named validation.cs:

public static class Validation
{
public const string EmailStandard = @"^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$";
public static bool ValidateEmail(string emailID)
{
if (emailID != null)
return System.Text.RegularExpressions.Regex.IsMatch(emailID, EmailStandard);
else
return false;
}
}



Now from our aspx page write the below code when user click on a button to proceed.

if (Validation.ValidateEmail(txtEmail.Text))
{
//Valid Email
}
else
{
//Invalid Email
//Notify user
return;
}


Hope now readrers can easily implement the both javascript & server side validation & also can modify.

3 comments:

U.R.Pratibha said...

hi shwapnendu,

I m working on similar lines, but i wanted to know if u cud help me in checking if an email id exists or not !!!
The expression is correct but say if - username@gmail.com account exists or not

Saion Roy said...

Hi PRATS,
You will get a lot of free web service but i am little bit confused because continuous checking of google, yahoo, msn id may be your IP will be black listed. Thats why if you look at the famous sites they activate the user account by sending a mail with an activation link.

So i think it will be better if you want to keep your contacts clean follow the above technique.

If you need badly then you can try with VRFY(most mail server admin disables this feature for spammers) function of SMTP. For more details read rfc of SMTP.

mohammad said...

hi thank u alot it helps me alot

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash