The Brave Programmer - Blogging and coding
Not for the faint hearted
 

Blog Posts From The Brave Programmer

Minimize

Validating email addresses with regular expression (Regexp)

Apr20

Written by:
2009/04/20 08:06 AM RssIcon

How many of you guys have web forms on your websites that require users to fill in. Part of those forms require information to be valid. Things like  phone numbers, email addresses.  Validating these form fields can be a task in and of itself. Many have always resorted to server side validation. But that requires a round trip to the server. Is there an easier way to do this? Can this be done on the client side, before they submit the web form to the server.

Well thankfully we have something called regexp, or Regular expression. A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager. The regex equivalent is .*\.txt$.

Regular expressions are used by many text editors, utilities, and programming languages to search and manipulate text based on patterns. For example, we can use Regular Expression to check whether the user input is a valid ID number, a valid phone number or a valid password , etc.  We can use this functionality to validate against email addresses filled on a web form. We want to make sure that our email field in our form complies to a valid email address. It must have an “@”, a period, a domain, etc.

I came across a nice little regular expression article that gives us a nice concise expression to use for email addresses. http://www.zparacha.com/ultimate-java-regular-expression-to-validate-email-address/ Although it is a website that deals primarily with Java, the regexp will work with any language that implements regular expression libraries.

The main regular expression looks like this:

"^[\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Z]{2,4}$"

Following email addresses will pass validation

On his site, Zaheer Paracha,  explains a lot on the make up of this regular expression. You can find it on his blog about Java Regex Validation. But I am going to post an insert here from that particular post. Make sure you read it to get the full gist of the regexp explanation.

Email format: A valid email address will have the following format:

  • [\\w\\.-]+ : Begins with word characters, (may include periods and hyphens).
  • @ : It must have a ‘@’ symbol after initial characters.
  • ([\\w\\-]+\\.)+ : ‘@’ must follow by more alphanumeric characters (may include hyphens.).
    This part must also have a “.” to separate domain and sub domain names.
  • [A-Z]{2,4}$ : Must end with two to four alphabets.
    (This will allow domain names with 2, 3 and 4 characters e.g pa, com, net, wxyz) - Zaheer Paracha

There are so many rules and implementations of regexp that it is out of the scope of this post to deal with them all. But below you will find some links to other websites that will explain and give you a lot more information and examples

http://www.regular-expressions.info/

http://www.greenend.org.uk/rjk/2002/06/regexp.html

http://www.w3schools.com/jsref/jsref_obj_regexp.asp

pimp it

Tags:
Categories:
blog comments powered by Disqus
 
Blog Updates Via E-mail
 Blog Updates Via E-mail
Minimize

Do you want to receive blog updates via e-mail. Then just click on the link below. You will be redirected to Google's feed burner, where you can fill out a form. Supplying your e-mail address.

The subscription is managed entirely by Google's Feedburner. We cannot and do not collect your email address.

Subscribe to The Brave Programmer by Email

Print  
 

 

Latest Comments
 Latest Comments
Minimize
Powered by Disqus

Sign up with Disqus to enjoy a  surprise box of features

Print  
 
Blog Roll
 Blog Roll
Minimize
Print  
 
Categories
 Categories
Minimize
Print  
 
<h1>Search Blogs From The Brave Programmer</h1>
 

Search Blogs From The Brave Programmer

Minimize
Print  
 
Archive
 Archive
Minimize
Archive
<December 2024>
SunMonTueWedThuFriSat
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234
Monthly
Go
Print  
 
<h1>News Feeds (RSS)</h1>
 

News Feeds (RSS)

Minimize
Print  
 

Follow robertbravery on Twitter

Blog Engage Blog Forum and Blogging Community, Free Blog Submissions and Blog Traffic, Blog Directory, Article Submissions, Blog Traffic

View Robert Bravery's profile on LinkedIn

Mybyte

 

Robert - Find me on Bloggers.com

Tags
 Tags
Minimize
Print  
 
Contact Us Now
 Contact Us Now
Minimize
 

Email  us now or call us on 082-413-1420,  to host your website.

We design and develop websites. We develop websites that make a difference. We do Dotnetnuke Module development.

Web Masters Around The World
Power By Ringsurf
Print