ratcateme Posted July 30, 2008 Share Posted July 30, 2008 I am wanting to validate emails i have searched all over google and stuff i can find plenty of ways to validate emails like test@test.com but they wont validate emails like test@test as well. i am just looking for a expression to validate both at the same time or i can do each like this if(preg_match(test@test) || preg_match(test@test.com)) so far i have this ^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$] i tried to modify it to ^[a-zA-Z0-9_]+@[a-zA-Z0-9\-\.]+$] but that wont work. Scott. Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 30, 2008 Author Share Posted July 30, 2008 i have got this now ^[a-zA-Z0-9_]+@+[a-zA-Z0-9_] but it accepts asd_asd@te@st.com as valid Scott. Quote Link to comment Share on other sites More sharing options...
sasa Posted July 30, 2008 Share Posted July 30, 2008 try <?php $test = array('test@test', 'test@test.com', 'test@test@test.com', 'john@google.com.com'); foreach ($test as $a){ echo $a, ' - '; echo preg_match('/^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]+$/', $a), "<br />\n"; } ?> Quote Link to comment Share on other sites More sharing options...
ratcateme Posted July 31, 2008 Author Share Posted July 31, 2008 thanks for that but my site deals with emails like john@users.sourceforge.net and that says a three part domain is invalid. Scott. Quote Link to comment Share on other sites More sharing options...
effigy Posted July 31, 2008 Share Posted July 31, 2008 How about this? /^\w+@(?:\w+\.)+\w+$/ Quote Link to comment Share on other sites More sharing options...
corbin Posted August 1, 2008 Share Posted August 1, 2008 How about this? /^\w+@(?:\w+\.)+\w+$/ 1 or more letters/numbers @ 1 or more letters/numbers . 1 or more letters/numbers What about john.smith@host.com, or john@users.sourceforge.net (using the earlier example)? Or am I reading the regexp incorrectly? Quote Link to comment Share on other sites More sharing options...
ratcateme Posted August 1, 2008 Author Share Posted August 1, 2008 yea on testing i found john@users.sourceforge.net doesn't work and john.users@sourceforge.net wont work any other ideas Scott. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 3, 2008 Share Posted August 3, 2008 Try /^(?:\w+.?)+@(?:\w+.?)+$/ Quote Link to comment Share on other sites More sharing options...
unkwntech Posted August 3, 2008 Share Posted August 3, 2008 I'm more interested as to why you want to validate test@test. This is based on the RFC standard and should catch ANY valid email. (?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(??:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]) Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 3, 2008 Share Posted August 3, 2008 I'm more interested as to why you want to validate test@test. Because the syntax is user@hostname. Thus if the hostname is daniel-laptop and the user is daniel then the address would be daniel@daniel-laptop. If the hostname is gmail.com and the user is daniel.egeberg then the address is daniel.egeberg@gmail.com. Simple, right? Example: daniel@daniel-laptop:~$ mail No mail for daniel daniel@daniel-laptop:~$ mail daniel@daniel-laptop Subject: Test Email This is a test Cc: daniel@daniel-laptop:~$ mail Mail version 8.1.2 01/15/2001. Type ? for help. "/var/mail/daniel": 1 message 1 new >N 1 daniel@daniel-lap Sun Aug 3 20:13 14/476 Test Email & Quote Link to comment Share on other sites More sharing options...
PHPTOM Posted August 3, 2008 Share Posted August 3, 2008 if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ echo "not valid"; } PHP 5 only ---------------- Now playing: Linkin Park - Papercut via FoxyTunes Quote Link to comment Share on other sites More sharing options...
ratcateme Posted August 3, 2008 Author Share Posted August 3, 2008 i put that expression into the code above i think i got the escaping wright here's what i go preg_match("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")@(??:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(??:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])", $a) it said Unknown modifier '@' i tried PHPTOM's suggestion but that says test@test is not valid Scott. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 3, 2008 Share Posted August 3, 2008 You need a delimiter. Quote Link to comment Share on other sites More sharing options...
ratcateme Posted August 4, 2008 Author Share Posted August 4, 2008 You need a delimiter. where should i put it i am still learning Regex Scott. Quote Link to comment Share on other sites More sharing options...
nrg_alpha Posted August 4, 2008 Share Posted August 4, 2008 While this solution is not so much hardcoded preg expression stuff, it does the trick. This is what I ran into recently: $email = filter_var(filter_var($_POST['email'], FILTER_SANITIZE_EMAIL), FILTER_VALIDATE_EMAIL); if($email === false){ return false; } else { return true; } One big area of concern with expressions is how 'strict' some can be. The general consensus is that allowing some flexibility is more preferable than locking out potential email validations due to extrememly tight-fisted expression routines. So PHP has these nice and neat little FILTER_SANITIZE_EMAIL and FILTER_VALIDATE_EMAIL functionalities that make for some relaxed enough checking to as to be reasonable enough, yet be strict enough at the same time... Cheers, NRG Quote Link to comment Share on other sites More sharing options...
ratcateme Posted August 4, 2008 Author Share Posted August 4, 2008 thanks for the reply but that still says test@test is invalid. does anyone know how i could use this one eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) with another one that will validate emails like test@test so my script look like if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email) || preg_match(test@test)) Scott. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted August 4, 2008 Share Posted August 4, 2008 What's wrong with the one I gave you? I tested it and it does match the emails you want: Try /^(?:\w+.?)+@(?:\w+.?)+$/ function test($email) { echo $email . ': '; if (preg_match('/^(?:\w+.?)+@(?:\w+.?)+$/', $email)) { echo 'valid'; } else { echo 'invalid'; } echo PHP_EOL; } test('test@test'); test('test.hello@test'); test('test@test.hello'); test('test.hello@hello.test'); Output: test@test: valid test.hello@test: valid test@test.hello: valid test.hello@hello.test: valid By the way, the PCRE functions (e.g. preg_match()) are faster than the POSIX regex functions (e.g. ereg()). Edit: Actually, it should be /^(?:\w+\.?)+@(?:\w+\.?)+$/ instead. Otherwise the period (.) will act as a metacharacter meaning "any character", but we want it to be a literal period. Quote Link to comment Share on other sites More sharing options...
ratcateme Posted August 5, 2008 Author Share Posted August 5, 2008 the problem with the first way you posted it said emails like bob@users.sourceforge.net or bob.users@sourceforge.net but your new update works better and says all the emails i want are valid Scott. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.