CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: validating an email address  (Read 7356 times) Bookmark and Share
JennyJenny
Newbie
*
Posts: 6



View Profile
« on: Dec 08, 2010, 11:59:10 am »

How can I validate an email address in php?
Logged
Mr. Crane
Jr. Member
*****
Posts: 63



View Profile WWW
« Reply #1 on: Dec 08, 2010, 12:21:52 pm »

How can I validate an email address in php?

Take a look at these snippets:

Validate email address #1
Validate email address #2
Validate email address #3

One of those ought to do it. Smiley

This is the third one, which looks okay:

Code:  
Highlight Mode: (PHP)
  1. <?PHP
  2.  
  3. function isValidEmail( $email = null ){
  4.     return preg_match( "/^
  5.     [\d\w\/+!=#|$?%{^&}*&#38;#x60;'~-]
  6.     [\d\w\/\.+!=#|$?%{^&}*&#38;#x60;'~-]*@
  7.     [A-Za-z0-9]
  8.     [A-Za-z0-9.-]{1,61}
  9.     [A-Za-z0-9]\.
  10.     [A-Za-z]{2,6}$/ix", $email );
  11. }
  12.  
  13. ?>
 
« Last Edit: Dec 09, 2010, 12:18:05 pm by Mr. Crane » Logged

Traveling through the Digital Age at 33⅓rd revolutions per minute.
Nico
Newbie
*
Posts: 5



View Profile WWW
« Reply #2 on: Jan 11, 2011, 01:19:11 pm »

You can also use PHP's built-in function.

Code:  
Highlight Mode: (PHP)
  1. if (filter_var($input, FILTER_VALIDATE_EMAIL))
  2. {
  3.    // Valid
  4. }
 

php.net/filter_var
Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #3 on: Jan 11, 2011, 01:27:04 pm »

Lol, wait, that's too easy. Smiley  Dang, now I gotta go read up on the FILTER_VALIDATE stuff.

You can also use PHP's built-in function.

php.net/filter_var
« Last Edit: Jan 13, 2011, 08:14:02 am by Max » Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #4 on: Jan 13, 2011, 06:49:59 am »

PHP version has to be 5.2.0+ for filter_var

You can use this code if you have to use preg_match now, but would like to use filter_var in the future.

Code:  
Highlight Mode: (PHP)
  1. <?php
  2.  
  3. $email='this@that.com';
  4. if (function_exists('filter_var'))
  5. {
  6.        echo 'Using filter_var: ';
  7.        $bValid = filter_var($email,FILTER_VALIDATE_EMAIL);
  8. }
  9. else
  10. {
  11.        echo 'Using preg_match: ';
  12.        $bValid = preg_match("/^([A-Za-z0-9\.\_\%\+\-]{1,39})@([a-zA-Z0-9\.\-]{2,34})\.[a-zA-Z]{2,5}$/",$email);
  13. }
  14. if (!$bValid)
  15.        echo 'Invalid';
  16. else
  17.        echo 'Valid';
 
Logged
UnrealEd
Newbie
*
Posts: 22



View Profile
« Reply #5 on: Jan 13, 2011, 11:42:10 am »

I think it's easier if you create the filter_var function when it doesn't exist. It adds functionality and reusability of the code (suppose you need to validate email addresses on other pages as well):

Code:  
Highlight Mode: (PHP)
  1. if (!function_exists ('filter_var')) {
  2.  # define the VALIDATE FILTER-constants you would like to use, more info on php.net
  3.  define ('FILTER_VALIDATE_EMAIL', '/^([A-Za-z0-9\.\_\%\+\-]{1,39})@([a-zA-Z0-9\.\-]{2,34})\.[a-zA-Z]{2,5}$/');
  4.  define ('FILTER_VALIDATE_BOOLEAN', '/^(1|yes|true|on)$/i');
  5.  
  6.  # the filter_var function
  7.  function filter_var ($variable, $filter) {
  8.       return preg_match ($filter, $variable)? $variable : FALSE;
  9.  }
  10. }
  11.  
 

As you can see I left out the optional options argument, but that's just because I haven't looked into the filter_var function yet, so I don't really know the possible values of this argument. When I do, I'll update the function

Currently my filter_var function only allows validate-filters, but it is possible to extend the function to allow sanitize and other filters (as defined by php.net). This would require a switch statement in the function filter_var that will call different functions (preg_match, preg_replace), depending on the value of the filter argument.
Logged
phpMan2010
Newbie
*
Posts: 32



View Profile
« Reply #6 on: Jan 13, 2011, 12:41:45 pm »

Very cool.  Thanks!

Smiley
Logged
Tags:
Pages: 1
  Print  
 
Jump to: