Click to See Complete Forum and Search --> : [RESOLVED] REGEX to validate email inputs


toraj58
October 4th, 2008, 04:58 AM
i need a regex to validate the email input in the textbox of the form that end users enter.

PeejAvery
October 4th, 2008, 07:51 AM
You need to learn to start using Google. It can find plenty of solutions so that you don't have to wait upon us to reply.

http://www.google.com/search?hl=en&q=regular+expressions+validate+e-mail+php&btnG=Search

toraj58
October 4th, 2008, 09:57 AM
thanks; i googled it!
i just paste the code here for other lazy developers like me:D


<?php

$email = "someone@example.com";

if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
echo "Valid email address.";
}
else {
echo "Invalid email address.";
}

?>

toraj58
October 6th, 2008, 09:31 AM
the previous post that i did uses POSIX function i investigated more and found that PCRE (Perl Compatible Regular Expression) functions are faster and more popular that POSIX functions, so i post the PCRE code here also as a better solution for whom care to this thread:


<?php
if ($submit) {
$okay = preg_match(
'/^[A-z0-9_\-]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{2,4}$/',
$emailfield
);
if ($okay) {
echo "E-mail is validated";
} else {
echo "E-mail is incorrect";
}
}else {
?>
<form method="POST" action="email.php">
E-mail address: <input type="text" name="emailfield">
<br><input type="submit" name="submit" value="Validate">
</form>
<?php
}
?>


Touraj Ebrahimi [toraj_e] [at] [yahoo] [dot] [com]