Jump to content

Preg_match


xyn

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]
Link to comment
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]
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.