Published in: PHP
function is_valid_email($email) { return true; else return false; }
Comments
Subscribe to comments
You need to login to post a comment.
Wicked Cool PHP: Real-World Scripts That Solve Difficult Problems
Wicked Cool PHP contains a wide variety of scripts to process credit cards, check the validity of email addresses, template HTML, and serve dynamic images and text.
tylerhall on 12/31/69
127 people have marked this snippet as a favorite
arcturus
Bunker
College
theferf
mrm52
luxuryluke
designerd
katxorro70
FReigM
terriK
px
yuconner
irdial
olive
oronm
NexusRex
ttscoff
clifton
raws
blakeb
Hollow
dmarten
demods
noname
banjomamo
Phoenix
alexd0001
bitcrumb
fael
rich13
edwinjanmoss
vali29
hudge
copyleft
motoroller
dobbshead
marteki
verbal
vilebender
adamweeks
madrid
mbcdg
jeff
raptrex
emuman
JimiJay
cschlens
xsubodh
cristianciofu
sosof
SpinZ
Firemarble
dyesin
adamsimms
thebrokenlight
eorwoll
paullorentzen
ibomb
mrjthethird
Arzakon
skywalker
zeusmedia
haozi
romanos
jamarama
Alshie
gAmUssA
mmccrack
stasiaholdren
unabatedshagie
blackabee
shii
dopple
fsorbello
pablodgavilan
jamesming
tikitakfire
jsalo
crashdr
amxm
sumandahal
darkapple
jfherring
oriolfb
hans
Leech
shameel
grassdog
Shocm
Wiederkehr
rubensarrio
ahjo
meetneps
joaosalless
salibaray
iconsis
jackii
baqc
carlosabargues
LostCore
justinscheetz
Hilyin
joelataylor
yuindustries
brother_maynard
vevhlos
elbuenob
PapTom
polarbear
windmarble
owais
cornellsteven
Anber
nreliu
irishsk
hamiltonmascioli
Triconium
g0mer
thadwheeler
Zaphod_42
fragmentist
ozone
nb109
delarge
Nanobyte
metallic07039
gripnrip
Published in: PHP
function is_valid_email($email) { return true; else return false; }
Subscribe to comments
You need to login to post a comment.
The regular expression
[a-zA-Z0-9_-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+
does not compile.
When a dash is used in a character class it indicates a range.
_-. is not a valid range.
A corrected version could be
[.+a-zA-Z0-9_-]+@[a-zA-Z0-9-]+.[a-zA-Z]+
or
[-a-zA-Z0-9_.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+
When the dash is moved to the beginning or end of the character class it is automatically not part of a range and instead does a literal match on the dash character.
Here are my sample scripts:
email_bad.php:
!/usr/bin/php
email_fixed.php:
!/usr/bin/php
Doesn't seem to allow PHP tags. Here's another try on those sample scripts:
Here are my sample scripts:
email_bad.php:
errorreporting( EALL );
function isvalidemail( $email ) { if( pregmatch( "/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email ) > 0 ) return true; else return false; }
$emails = array( 'br0k3n', 'fred_dred@aol.com', 'fred-dred@aol.com', 'fred.dred@aol.com', 'fred+dred@aol.com' );
foreach( $emails as $email ) { echo $email . ' is' . ( isvalidemail( $email ) ? '' : ' not' ) . " valid\n"; }
email_fixed.php:
errorreporting( EALL );
function isvalidemail( $email ) { return pregmatch( '/[.+a-zA-Z0-9-]+@[a-zA-Z0-9-]+.[a-zA-Z]+/', $email ); }
$emails = array( 'br0k3n', 'fred_dred@aol.com', 'fred-dred@aol.com', 'fred.dred@aol.com', 'fred+dred@aol.com' );
foreach( $emails as $email ) { echo $email . ' is' . ( isvalidemail( $email ) ? '' : ' not' ) . " valid\n"; }
Simplest way. PHP5 only
http://www.w3schools.com/php/filtervalidateemail.asp
http://snipplr.com/view/8844/rfc822-compliant-email-address-validator/
http://snipplr.com/view/11616/rfccompliant-email-address-validator/
And check out a head-to-head of free validators here: http://www.dominicsayers.com/isemail/
From w3schools - http://www.w3schools.com/php/filtervalidateemail.asp