Jump to content

Archived

This topic is now archived and is closed to further replies.

xyn

Preg_match

Recommended Posts

Hii

Does anyone know how to make my code correct so an e-mail MUST be filled in correctly:

[code]$email = $_POST['mail'];
if( !preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+/", $email)){
die('error');
}
[/code]

Share this post


Link to post
Share on other sites
I suggest looking into the PEAR package, Validator - or something like that. I use it to verify emails and it works like a charm. Even if you don't want the extra code, it is worth checking out to see what regular expression they use.

Share this post


Link to post
Share on other sites
[!--quoteo(post=389268:date=Jun 29 2006, 03:42 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 29 2006, 03:42 PM) [snapback]389268[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I suggest looking into the PEAR package, Validator - or something like that. I use it to verify emails and it works like a charm. Even if you don't want the extra code, it is worth checking out to see what regular expression they use.
[/quote]
I took a 2nd glance and realised i forgotte to put the .com // .co.uk etc. part so it should have looked like:

[code]
$mail = $_POST['mail'];
if( !preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.[A-Za-z0-9\]/", $mail)){
die('error: no email');
}
[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=389274:date=Jun 29 2006, 10:50 AM:name=Ashh)--][div class=\'quotetop\']QUOTE(Ashh @ Jun 29 2006, 10:50 AM) [snapback]389274[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I took a 2nd glance and realised i forgotte to put the .com // .co.uk etc. part so it should have looked like:

[code]
$mail = $_POST['mail'];
if( !preg_match("/[A-Za-z0-9\._%-]+\@[A-Za-z0-9\._%-]+.[A-Za-z0-9\]/", $mail)){
die('error: no email');
}
[/code]
[/quote]

about the best email validation i've found is this:
[code]
preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $email);
[/code]

the only things you're really missing is the '^' at the beginning and the '$' at the end to make sure that nothing funky has been going on in front or behind it.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.