function is_valid_email($email) { return true; else return false; }
Comments
Subscribe to comments
You need to login to post a comment.
tylerhall on 11/30/-1
305 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
wirjo
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
matthall
owais
cornellsteven
Anber
willwish
nreliu
novatvstdios
irishsk
hamiltonmascioli
Triconium
g0mer
thadwheeler
Zaphod_42
fragmentist
ozone
nb109
delarge
Nanobyte
metallic07039
gripnrip
thatryan
eapen
Blux
JustGreg
obsessivejosh
maxvee8
unitechy
calvingilbert
NyX
joet3ch
aha0617
mailstefsteide
tandouri
GrillPhil
rene-design
zartgesotten
codearachnid
jweagley
KF
Gr33d
Blacksnipe
buk
mattnews
palimadra
kellyrmartin
ruhanirabin
shabbar
debagel
risico
cindreta
achilles283
ckayra
franverona
portalpie
eivind
metoikos
dfaulty
bradless
jaff
larste
benrudolph
LyndseyPearce
leecsargent
webalienz
jkjeldgaard
sandynata
Trinovantes
hades985
d55beck
kaartz
phil7085
yves
kwanhon
jofalltrades
sethetter
sonnh
joaofgf
htl
NARKOZ
ofadlaoui
osirisinternet
sree01
iamadams
giuliom
lolindirfaelivrin
Swift-R
sxnc
mgerdt
all3n
ATLChris
pytheas
jimridge
daubu
pchengsf
techdetours
naeemnur
johnamiahford
yauuik
fauverism
athanassiadis
fernandojmartin
gnitter
pinyeiro
cjmling
adrianspiegel
seanpowell
bobbym245
vkolev
armanx
gavatron
dext
kuchenfari
bigfish
bindaskhan2004hotmailcom
lifewishes
zachdunn
poet
sherlack
pablo808
connorjackson
claudiowebdesign
vaskointeractive
0leg
williamsOwen
RvDesigns
broikmann
jarod51
arsha
tspitzr
aegony
garthhumphreys
dwenaus
creativeboulder
kijtra
CyKy
bdiddymc
intothelight
NeekGerd
thegnobo
silverskymedia
metthyn
dubogii
avinash2
anhonestboy
luckystokes
julianrhyswilliams
djsoftlayer
wearefreya
germanny
prit
rajashri78
logiq
qubestream
benediktvaldez
Corncrib
jumichot
dayseye49
merritt212
18zehn
Ideandro
MadRukus
Briantjuh
xhiena
nerdfiles
ban17
rlynch3
XjSv
fjckls
depiction
meetzah
DesignGoggles
svemoguci
geek4evr
TomasBerckx
gregorynicholas
fengelz
kript
tanveer
timr
RainyDayMedia
sharktale
tralston
mr2p
rweng
jasonlindberg
masta
andrey
White
bug5
speckledjim
rookie68
owenbot
dhargan
rmethod
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
To check whether an email address actually exists on the mailserver, use the [PHP SMTP Email Validation][1] class.
[1] http://code.google.com/p/php-smtp-email-validation/ "PHP SMTP Email Validation"
The correct link:
To check whether an email address actually exists on the mailserver, use the PHP SMTP Email Validation class.
Simplified reg ex for this: /\S+@\S+.\D+/
Try this: validemail.org
Here is another SMTP mail validation function in php: http://snipplr.com/view/25505/validate-emails-with-smtp-and-php/
You can try the next: function checkemailaddress($email) { // First, we check that there's one @ symbol, // and that the lengths are right. if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) { // Email invalid because wrong number of characters // in one section or wrong number of @ symbols. return false; } // Split it into sections to make life easier $emailarray = explode("@", $email); $localarray = explode(".", $emailarray[0]); for ($i = 0; $i < sizeof($localarray); $i++) { if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_
{|}~-][A-Za-z0-9!#$%& ↪'*+/=?^_{|}~.-]{0,63})|(\"[^(\|\")]{0,62}\"))$", $local_array[$i])) { return false; } } // Check if domain is IP. If not, // it should be valid domain name if (!ereg("^[?[0-9.]+]?$", $email_array[1])) { $domain_array = explode(".", $email_array[1]); if (sizeof($domain_array) < 2) { return false; // Not enough parts to domain } for ($i = 0; $i < sizeof($domain_array); $i++) { if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])| ↪([A-Za-z0-9]+))$", $domain_array[$i])) { return false; } } } return true; }Also you can search for demo email php form and look a right code
apart from the little typos/errors noted by the others, this is a great snippet. Short, Sweet, and very useful. thanks and good work, dude - keep it up!!
-darksider-
http://www.snipplr.com/view/10897/valid-email-address-filter/
+1 for @jfherring
The code is unnecessarily verbose. There's no need to explicitly return true/false with an if/else. This body will suffice:
return (pregmatch("/[a-zA-Z0-9-.+]+@[a-zA-Z0-9-]+.[a-zA-Z]+/", $email) > 0);
Way too much of an overkill. You should be using PHP's filter_var. Example below:
if(!filtervar($email, FILTERVALIDATE_EMAIL)) { // Error message here }
@NaRzY - that code is nearly 3 years old.
there is no reason to use if/else statement you can simply return preg_match(); http://snipplr.com/view/45702/check-for-valid-email-address-rfc/
http://snipplr.com/view/45716/check-for-valid-email-address-rfc/
As many have noted, using custom regular expressions isn't necessary when filter_var options are available.
+1 to housecor as well.
filtervar($email, FILTERVALIDATE_EMAIL);
This either returns the email or false.
http://www.snipplr.com/view/58338/validate-email-and-domain/
http://snipplr.com/view/40841/email-validation-ipv6-version/ && http://snipplr.com/view/40842/email-validation-ipv4-version/
both would do a better job, however, unless you are forced due to legacy reasons, I would use php's built in filtervar() --> http://us.php.net/manual/en/function.filter-var.php ( some people are still running pre 5.2 versions of php )
you should also check for a valid DNS
function validate_email($email){
$exp = "^[a-z\'0-9]+([.-][a-z\'0-9]+)*@([a-z0-9]+([.-][a-z0-9]+))+$";
if(eregi($exp,$email)){
}else{
}
}