Jump to content


Photo

Preg_match


  • Please log in to reply
3 replies to this topic

#1 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 29 June 2006 - 02:38 PM

Hii

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

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


#2 Buyocat

Buyocat
  • Members
  • PipPipPip
  • Advanced Member
  • 267 posts

Posted 29 June 2006 - 02:42 PM

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.
Looking for some easy-to-use tools?  Try these, https://sourceforge....jects/utils-php -- I made them myself.  They're distinct tools which are easy to understand and use.  See some examples uses at http://www.anotherearlymorning.com

#3 xyn

xyn
  • Members
  • PipPipPip
  • Advanced Member
  • 779 posts
  • LocationNorthampton

Posted 29 June 2006 - 02:50 PM

[!--quoteo(post=389268:date=Jun 29 2006, 03:42 PM:name=Buyocat)--][div class=\'quotetop\']QUOTE(Buyocat @ Jun 29 2006, 03:42 PM) View Post[/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:

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


#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 June 2006 - 03:29 PM

[!--quoteo(post=389274:date=Jun 29 2006, 10:50 AM:name=Ashh)--][div class=\'quotetop\']QUOTE(Ashh @ Jun 29 2006, 10:50 AM) View Post[/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:

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

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

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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users