Pages

Wednesday, April 29, 2009

Validate Email Address using Regular Expression Control in ASP.NET

In this article i would like to show you a simple code to validate User input to accept Email Address in Text Box control.

The traditional approach is to OnLostFocus event of Text Box we call a Java Script to read all the characters on by one to validate the entered the input valid email address or not. That is looking for @ symbol and using some blind rules.

Its really difficult to validate email address because now a days we see somany domains providing unique way to create email address. This is time consuming process to validate all such cases. Instead if you are familiar with Regular Expression the it becomes your life easier.So here i am going to give you simple example to validate email address in text box.

Lets look at the below code:


I have one Text Box with the ID txtEmailAddr and one RegularExpressionValidator where ControlToValidate is txtEmailAddr and ValidationExpression is "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" and If the format of the email doesn't match with regular expression then throw "Enter Valid Email Address" error message.
Very simple isn't it :)....
Similar Articles:

No comments: