Click to See Complete Forum and Search --> : Great email verification
slimsam1
05-22-2003, 12:34 AM
Thought I'd share this with everyone, I wrote it the other day and I like it a lot:
function verifyemail($anchormail,$anchordescription = " ")
{
$domain = explode("@",$anchormail);
if (ereg("^[a-zA-Z0-9_\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$", $anchormail)
&& ereg("^[a-zA-Z0-9_\@\.\,\ ]+$", $anchordescription)
&& checkdnsrr($domain[1]) ) {
return TRUE;
} else {
return FALSE;
}
}
Notice the checkdnsrr($domain[1]) :)
it actually looks for MX records on the domain... really cool.
jeffmott
05-22-2003, 01:08 AM
d-lewart@uiuc.edu: a valid address that would fail
...@yahoo.com: an invalid address that would pass
Here's a regex derived from RFC822: Standard for ARPA Internet Text Messages. Obviously more complex, but nothing short of perfect. :)/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/This regex was tested in Perl, so some characters preceeded with a backslash (such as @) may not need be in PHP.
slimsam1
05-22-2003, 03:00 AM
<?
function email($emailAddress,$emailDisplay = " ")
{
if (preg_match("/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/", $emailAddress)){
$emailArray = explode("@",$emailAddress);
if (checkdnsrr($emailArray[1])){
return TRUE;
}
}
return FALSE;
}
?>
this didn't work.... :-/ I can't seem to figure out what's wrong. I tried changing all \\" to \" ..... no luck.
Here's the PHP version:
'/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i'
slimsam1
05-22-2003, 01:24 PM
Thanks! Excellent.
class verify
{
function email($emailAddress)
{
if (preg_match('/[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+/i', $emailAddress)){
$emailArray = explode("@",$emailAddress);
if (checkdnsrr($emailArray[1])){
return TRUE;
}
}
return FALSE;
}
}
Nevermore
05-25-2003, 11:35 AM
Can you have two @ signs in an email address? Only, I don't think you can, and that script lets it through anyway.
jeffmott
05-25-2003, 11:53 AM
Could you provide a more specific example of where you think it fails?
Note also that if you match against a string such as "hello@world@some.thing" that what really is being matched is...
hello@world@some.thing
This is the valid address it finds and thus returns true. You'll have to specify that the match must start and end from the beginning to the end of the string. That's done with the ^ and $ characters, e.g.,/^...regex here...$/
Nevermore
05-25-2003, 11:58 AM
Can't you check that only one @ sign is being used by this code:
$emailArray = explode("@",$emailAddress);
if (checkdnsrr($emailArray[2])){
//Fail Code
}else {//Succeed code
}
jeffmott
05-25-2003, 12:08 PM
Can't you check that only one @ sign is being used by this codeNo, actually. Consider blah@blah@blah.blah
Your conditional would then be checking checkdnsrr("blah.blah"), which would return false and execute your succeed code.
But if you make the change I recommended then there's no need to perform the additional check at all.
Nevermore
05-25-2003, 12:10 PM
Thanks for the tip - I'm no good with RegExps.
Nevermore
05-25-2003, 12:16 PM
I tried the redexp with the ^ and $ thing, and it just gives the error:
Warning: Unknown modifier '$' in /home/evernet/public_html/register.php.
With a line number, obviously.
I would like to use your regExp to validate email addresses on my site, could you help me please?
jeffmott
05-25-2003, 02:38 PM
I can't tell you where it might have gone wrong unless I see what you tried.
Nevermore
05-26-2003, 03:30 AM
It's working now, thanks. I had just used bad syntax when I added the extra bits.
mf22cs
05-28-2003, 05:38 PM
How about sharing the code that worked?
/Marcus - Newbie to PHP...
Are you talking the complete code or the regexp? Here is the regexp that will work:
/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i
and here some complete code:
<?PHP
if ($_POST['submit']) {
if (preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $_POST['email'])) {
echo ("valid");
}
else {
echo ("invalid");
}
}
?>
<html>
<head>
<title>Validate Email</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form name="myform" method="post" action="regexp.php">
<input type="text" name="email"/>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
mf22cs
05-28-2003, 06:19 PM
Thanx... that safed me a lot of nights sleep... :)
/Marcus
You're welcome, though credit goes to jeffmott for writing the regexp...
m_tyhurst2002
04-27-2008, 07:20 PM
Hello! I am using this code on my website. Is there anyway that I can add this email verification to my existing script so that I don't have to redo it all? Right now it just verifies that the email field is not empty. I have tried add the code on this forum thread but cannot get it to work. I would appreciate any kind of help that anyone may have. Thanks so much...
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/ DTD/xhtml1-transitional.dtd">
<?php
$Checked = false;
if (isset($_POST['submit'])) {
{
$name = $_POST['name'];
$email = $_POST['email'];
$itemrequested = $_POST['itemrequested'];
$color = $_POST['color'];
if ( !empty($name) &&
!empty ($email) &&
!empty($itemrequested) &&
!empty($color)) {
if(isset($_POST['submit'])) {
$to = "m_tyhurst2002@yahoo.com, pureascrystal@hotmail.com";
$subject = "Contact Form from www.mia-sofia.com/contact.php";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$phone_field = $_POST['phone'];
$itemrequested_field = $_POST['itemrequested'];
$color_field = $_POST['color'];
$socksize_field = $_POST['socksize'];
$message_field = $_POST['message'];
$bowsize_field = $_POST['bowsize'];
$headbandsize_field = $_POST['headbandsize'];
$body = "Information collected at www.mia-sofia.com/contact.php\n\n\n Name: $name_field\n\n E-Mail: $email_field\n\n Phone: $phone_field\n\n Item Requested: $itemrequested_field\n\n Color: $color_field\n\n Sock Size: $socksize_field\n\n Bow Size: $bowsize_field\n\n Headband Size: $headbandsize_field\n\n Message: $message_field";
$Checked = true;//Checked
mail($to, $subject, $body, "From:$email");
} else {
echo "blarg!";
}
}
else { $error = true; }
}
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Mia Sofia Accessories for Girls!</title>
<link href="styles.css" rel="stylesheet" type="text/css" />
<script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script>
</head>
<body>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="contactcontent">
<p align="center" class="contactheading">Order Form</p>
<br /><br />
<p align="center" class="contactformtext">Please fill out this form if you are interested in requesting a product or have a question. </p>
<p align="center" class="contactformtext"> </p>
<p align="center" class="contactformtext">We will contact you promptly in order to begin designing your unique Mia Sofia accessory.</p>
<p align="center" class="contactformtext"> </p>
<p align="center" class="contactformtext">Products will be mailed to you within 7-10 business days.</p>
<p> </p>
<p><span class="contactformthankyou">
<?php
if($Checked)
{
echo '<div align="center"><br>Thanks for your interest in our products ';
echo $_POST['name']."!<br /> We will contact you at, ";
echo $_POST['email']." promptly.<br><br></div> ";
}
?>
</span></p>
<br />
<table width="625" border="0" align="center">
<tr>
<td colspan="3" class="contactformthankyou"><span class="contactformerrortext">* Required</span></td>
</tr>
<tr>
<td width="8" class="contactformthankyou">*</td>
<td width="140" class="contactformtext">Name:</td>
<td width="438" class="contactformtext"><label>
<input type="text" name="name" id="name" value="<?php echo $name; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && empty($name) ) {
echo 'Please enter your first and last name.';
}
?>
</span></td>
</tr>
<tr>
<td class="contactformthankyou">*</td>
<td class="contactformtext">Email:</td>
<td class="contactformtext"><label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && empty($email) ) {
echo 'Please enter valid email address.';
}
?>
</span></td>
</tr>
<tr>
<td> </td>
<td class="contactformtext">Phone:</td>
<td class="contactformtext"><label>
<input type="text" name="phone" id="phone" value="<?php echo $phone; ?>" />
</label></td>
</tr>
<tr>
<td class="contactformthankyou">*</td>
<td class="contactformtext">Item:</td>
<td class="contactformtext"><label>
<select name="itemrequested">
<option selected="selected" value="">-Select-</option>
<option value="0">--Socks--</option>
<option value="Ribbon Socks">Ribbon Socks</option>
<option value="Beaded Socks">Beaded Socks</option>
<option value="Fun Fur Socks">Fun Fur Socks</option>
<option value="Hand Crocheted Socks">Hand Crocheted Socks</option>
<option value="0">--Hair Bows--</option>
<option value="Satin Bows">Satin Bows</option>
<option value="Organza Bows">Organza Bows</option>
<option value="Marabou Bows">Marabou Bows</option>
<option value="Grosgrain Bows">Grosgrain Bows</option>
<option value="0">--Head Bands--</option>
<option value="Lace Headbands">Lace Headbands</option>
<option value="Nylon Headbands">Nylon Headbands</option>
<option value="Braided Headbands">Braided Headbands</option>
<?php echo $itemrequested; ?></select>
</label><span class="contactformerrortext">
<?php
if ( $error==true && empty($itemrequested) ) {
echo 'Please select item of interest.';
}
?>
</span></td>
</tr>
<tr>
<td class="contactformthankyou">*</td>
<td class="contactformtext">Color:</td>
<td class="contactformtext"><label>
<input type="text" name="color" id="color" value="<?php echo $color; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && empty($color) ) {
echo 'Please enter color.';
}
?>
</span></td>
</tr>
<tr>
<td class="contactformthankyou"> </td>
<td class="contactformtext">Hair Bow Size:</td>
<td class="contactformtext"><label>
<select name="bowsize" id="bowsize">
<option value="">-Select-</option>
<option value="Small">Small</option>
<option value="Medium">Medium</option>
<option value="Large">Large</option>
<?php echo $bowsize; ?> </select>
</label></td>
</tr>
<tr>
<td class="contactformthankyou"> </td>
<td class="contactformtext">Sock Size:</td>
<td class="contactformtext"><label>
<select name="socksize">
<option value="">-Select-</option>
<option value="0-6">0-6 months</option>
<option value="6-18">6-18 Months</option>
<option value="18-36">18-36 Months</option>
<option value="3-5yr">3-5 Years</option>
<?php echo $socksize; ?></select>
</label></td>
</tr>
<tr>
<td valign="top"> </td>
<td valign="top" class="contactformtext">Headband Size: <br />
(Inches around head)</td>
<td align="left" valign="top" class="contactformtext"><label>
<input type="text" name="headbandsize" id="headbandsize" value="<?php echo $headbandsize; ?>" />
</label></td>
</tr>
<tr>
<td valign="top"> </td>
<td valign="top" class="contactformtext">Item Description:</td>
<td align="left" valign="top" class="contactformtext"><label>
<textarea name="message" id="message" cols="45" rows="5" /><?php echo $message; ?></textarea>
</label></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><label>
<input type="submit" name="submit" value="Submit" />
</label></td>
</tr>
</table>
<p> </p>
<p> </p>
</div>
</div>
</form>
</body>
</html>
Nevermore
04-28-2008, 02:17 AM
If you replace
!empty ($email)
with
preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email)
in your if() statement, you should have regexp matching on the email field. All credit to jeffmot for the regexp and Pyro for PHPing it.
Hope that helps.
m_tyhurst2002
04-28-2008, 06:20 AM
Thanks for your quick reply! I tried what you said above and initially it did not work. I added a "!" to the front of it and "&&" to the end and was able to view the page instead of just getting a blank white screen. Now that I see the page I can put "ert" in the email field and it still skips validating the email and sends the form. This is what I have now:
<?php
$Checked = false;
if (isset($_POST['submit'])) {
{
$name = $_POST['name'];
$email = $_POST['email'];
$itemrequested = $_POST['itemrequested'];
$color = $_POST['color'];
if ( !empty($name) &&
!preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email) &&
!empty($itemrequested) &&
!empty($color)) {
if(isset($_POST['submit'])) {
$to = "m_tyhurst2002@yahoo.com, pureascrystal@hotmail.com";
$subject = "Contact Form from www.mia-sofia.com/contact.php";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$phone_field = $_POST['phone'];
$itemrequested_field = $_POST['itemrequested'];
$color_field = $_POST['color'];
$socksize_field = $_POST['socksize'];
$message_field = $_POST['message'];
$bowsize_field = $_POST['bowsize'];
$headbandsize_field = $_POST['headbandsize'];
$body = "Information collected at www.mia-sofia.com/contact.php\n\n\n Name: $name_field\n\n E-Mail: $email_field\n\n Phone: $phone_field\n\n Item Requested: $itemrequested_field\n\n Color: $color_field\n\n Sock Size: $socksize_field\n\n Bow Size: $bowsize_field\n\n Headband Size: $headbandsize_field\n\n Message: $message_field";
$Checked = true;//Checked
mail($to, $subject, $body, "From:$email");
} else {
echo "blarg!";
}
}
else { $error = true; }
}
}
?>
And the PHP at the input field has been changed to:
<td class="contactformtext"><label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && !preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email) ) {
echo 'Please enter valid email address.';
}
?>
</span></td>
Thanks for your help. I have been messing with this stupid thing for hours now and can't find a solution. If it helps to look at the page you can here, http://mia-sofia.com/order.php Again, thanks so much for your help!!!
Nevermore
04-28-2008, 10:39 AM
I don't think you want the ! in front of the preg_match (in the form processor). The regular expression will return true if the address is a correctly formatted email address - your form will currently only let through invalid addresses.
m_tyhurst2002
04-28-2008, 11:13 AM
I don't think you want the ! in front of the preg_match (in the form processor). The regular expression will return true if the address is a correctly formatted email address - your form will currently only let through invalid addresses.
Do you mean I don't need it in the php script at the top of the page or the bit of php at the
<td class="contactformtext"><label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && !preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email) ) {
echo 'Please enter valid email address.';
}
?>
</span></td>
Sorry i feel so stupid when it comes to this stuff but I am trying to learn... It's going quite slowly... :confused: Thanks for your help
m_tyhurst2002
04-28-2008, 11:21 AM
I think I got it to work... Now I just need it to echo the error and show error message to the right of the field. This is what I thought it should be but it is not working for me...
<td class="contactformtext"><label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email) ) {
echo 'Please enter valid email address.';
}
?>
</span></td>
Thanks
Nevermore
04-28-2008, 03:03 PM
You need the ! there. This is confusing, isn't it? :D
When you're using an if() statement, the ! means not. So
if(x==true) means if x is true
if(x!=true) means if x is not true.
If you're using a function the ! goes before it:
if(myfunction()) executes if myfunction() returns true
if(!myfunction()) executes if myfunction() returns false.
So in your example:
<td class="contactformtext"><label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email) ) {
echo 'Please enter valid email address.';
}
?>
</span></td>
You want to test to see if
error is true
the email address was invalid
.
Since the regular expression returns true if the address is valid, we want to execute if the regexp returns false, hence:
<td class="contactformtext"><label>
<input type="text" name="email" id="email" value="<?php echo $email; ?>" />
</label><span class="contactformerrortext">
<?php
if ( $error==true && !preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $email) ) {
echo 'Please enter valid email address.';
}
?>
</span></td>
Incidentally, you don't need the curly braces; if you only have one operation to take place you can skip them:
if(x) doThis(); is equivalent to if(x) { doThis(); }
Hope that helps.
m_tyhurst2002
04-28-2008, 07:36 PM
Awesome, awesome, awesome. It worked!!! Thank you very, very much! I will name my first child Nevermore! :D