Home arrow PHP arrow Page 2 - Advanced PHP Form Input Validation to Check User Inputs

Validating Email Addresses - PHP

PHP form input validation is what separates amateur and professional PHP developers. A professional PHP developer validates data for both security and correctness of the data entered. Keep reading to learn how to validate user input to your forms.

TABLE OF CONTENTS:
  1. Advanced PHP Form Input Validation to Check User Inputs
  2. Validating Email Addresses
  3. Validating Domains for Email Addresses
  4. Validating Alphanumeric, Numeric and Alphabetic Input
By: Codex-M
Rating: starstarstarstarstar / 15
June 01, 2009

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Let's start by defining the email address. It contains two sections: the local part and the domain name. For example, in the email address Testuser1@xyz.com, Testuser1 is the local part of the email address while xyz.com is the domain name. Below are the specifications necessary for checking email address validity.

For the local part of the email address, a form should only permit:

  • Uppercase and lowercase English letters (a-z, A-Z)
  • The digits 0 through 9
  • The characters  ! # $ % & ' * + - / = ? ^ _ ` { | } ~
  • The character . provided that it is not the first nor last character; nor may it appear two or more times consecutively.

(Source: RFC Specification: http://en.wikipedia.org/wiki/E-mail_address )

The domain name half of the email address should be a valid domain. Validating an email address is very challenging, especially if you have a version of PHP below 5, because it requires a long Reg Ex syntax using either the ereg or pregmatch function. Luckily, with the release of PHP 5, you can easily validate email addresses using the filter_var / FILTER_VALIDATE_EMAIL function.

This will automatically check versus RFC specifications stated above. The last thing you need to check to find out if the email address is valid is whether the domain name given is valid. There are lots of approaches to this, including checking to see if the domain name has mail exchange records (MX) or A records.

You can find great resources relating to this task at: http://www.devshed.com/c/a/PHP/Email-Address-Verification-with-PHP/



 
 
>>> More PHP Articles          >>> More By Codex-M
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap

Dev Shed Tutorial Topics: