Email validation with regular expression


on Tuesday 27 June 2006
by Administrator author list email the content item print the content item
in Tutorials
comments: 2
hits: 20460
 1.6 - 3 votes -

[html] Introduction

Nowadays almost all website has some kind of html form. The most best-known are the user registration forms, information request forms and so on. As the form makes sense only if the visitor submits valid information so the site developer should take care of the data alidity.

The form validation process can be divided into 2 categories.
  • Client side validation
  • Server side validation

The client side validation is mostly realized via JavaScript code. The pro is that the processing doesn’t require any network transfer so it can be faster. The con is that the visitor can disable JavaScript in the browser and in this case he/she can submit invalid data.

That’s the point where the server side validation becomes important. The server side scripts can not be influenced by the visitor so you know that it will work in the same manner for all the visitors.
The best solution is if you apply both type of validation methods.


Validation technics

From this point on I will focus only on the server side validation of an email address. In PHP you receive the form fields values in the $_POST or
in the $_GET arrays. You must get the actual value to be checked from one of these arrays. When you have the correct variable you can try to run some validation routines on it.
There are again 2 main methods how you can check an email string:
  • String manipulation routines
  • Regular expressions


What to check

To make a good working validation routine you first need to clarify what is allowed and what is not. Let’s see how any email string should look like: test.user@demo.com
Now analyze this string a little bit. You should check the following points:

  • The string must contains one and only one ‘@’ character.
  • Before the ‘@’ it must have at least one character.
  • After the ‘@’ it must have a valid domain format with at least one ‘.’
  • The email can not contain any invalid character.
  • The total length of an email should be at least 6 character (a@b.us).




article index
page 1 - current : untitled page
page 2 : Page - 2
page 3 : Page - 3


Tags:

Php Toys - 2006 - Php resources, scripts and tutorials
Webhosting Info -
Insurance Index - Tutorial collection - HotScripts
Render time: 0.3562 second(s); 0.2371 of that for queries. DB queries: 49.