VALIDATING EMAIL ADDRESSES - May 29th, 2013


There are many times when the format of an email address needs to be validated.

There's a built in function in CMSB called isValidEmail that will do that for you, and might be used in the
errorsandAlerts section of your viewer like this:

<?php if (!isValidEmail(@$_REQUEST['email_address'])) { $errorsAndAlerts .= "Please enter a valid E-mail address
(example: user@example.com)\n"; } ?>

But to understand how it works, here's an regular expression approach that you can modify to suit your needs. They all
assume that the email address field is called email_address

To list all email addresses in an existing list with invalid formats:


<?php $count = 0; ?>
<?php foreach ($your_tableRecords as $record ): ?
<?
php // in the code \.[a-z0-9-]+)*(\.[a-z]{2,4}) 2 is the minimum and 4 is the maximum characters after the period to
account for TLDs like .us and .info
$email_address = $record['email_address'];
if(!
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email_address)) {

echo
$record['email_address'] ;
$count++;
}
?>
<?php endforeach ?>
<br /> <br />
There are <?php echo $count ?> invalid email addresses in this list.



If you want to list all email addresses with valid formats remove the ! from before eregi

You'll find many other useful regular expressions here:


http://www.virtuosimedia.com/dev/php/37-tested-php-perl-and-javascript-regular-expressions




The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service