{
....
}
$errorMessage = ""; // Always a good idea to initialize variables.
if ( is_valid_email ( $email, $errorMessage )
{
// good section
}
else
{
echo $errorMessage . "<br />" . PHP_EOL;
}
Does that work for you?
// apllication.php
if(is_valid_email($myEmail))
{
do something
}
// functions.php
function is_valid_email($email){
if(preg_match($regex, $email)){ $isvalidEmail = true; }
else{ $isvalidEmail = false; $errorMsg = "You Have entered an invalid email address."; }
if(mysql_num_rows($getEmailsInDbQuery) == 0){ $isAlreadyRegistered = true; }
else{ $isAlreadyRegistered = false; $errorMsg = "The email your are trying to use is already registered in our system."; }
if($isvalidEmail && $isAlreadyRegistered){ return true; }
else{ return false;}
}
<?php
function divide_integer ( $dividend, $divisor, &$remainder, &$errorMsg )
{
$errorMsg = "";
if ( ! is_int ( $dividend ))
{
$errorMsg = "dividend must be an integer";
return FALSE;
}
if ( ! is_int ( $divisor ))
{
$errorMsg = "divisor must be an integer";
return FALSE;
}
$remainder = $dividend % $divisor;
$quotient = ( $dividend - $remainder ) / $divisor;
return $quotient;
}
function test ( $dividend, $divisor )
{
$errorMsg = "";
echo "Attempting integer division on $dividend divided by $divisor" . PHP_EOL;
$quotient = divide_integer ( $dividend, $divisor, $remainder, $errorMsg );
if ( $quotient != FALSE )
{
echo "Quotient is $quotient" . PHP_EOL;
echo "Remainder is $remainder" . PHP_EOL;
}
else
{
echo "Error: $errorMsg" . PHP_EOL;
}
echo PHP_EOL;
}
test ( 20, 3 );
test ( 20.2, 3 );
test ( 20, 3.2 );
?>
Attempting integer division on 20 divided by 3
Quotient is 6
Remainder is 2
Attempting integer division on 20.2 divided by 3
Error: dividend must be an integer
Attempting integer division on 20 divided by 3.2
Error: divisor must be an integer
<?php // RAY_email_validation_with_message.php
error_reporting(E_ALL);
// A FUNCTION TO TEST FOR A VALID EMAIL ADDRESS, RETURN TRUE OR FALSE
// SEE MAN PAGE: http://php.net/manual/en/intro.filter.php
function check_valid_email($email)
{
// RETURNS A TWO-POSITION ARRAY
$return = array();
// LIST OF BLOCKED DOMAINS
$bogus = array
( '@unknown.com'
, '@example.com'
, '@gooseball.org'
)
;
// IF PHP 5.2 OR ABOVE, WE CAN USE THE FILTER
if (strnatcmp(phpversion(),'5.2') >= 0)
{
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE)
{
$return[0] = FALSE;
$return[1] = "FAILED FILTER_VAR()";
return $return;
}
}
// IF LOWER-LEVEL PHP, WE CAN CONSTRUCT A REGULAR EXPRESSION
else
{
$regex
= '/' // START REGEX DELIMITER
. '^' // START STRING
. '[A-Z0-9_-]' // AN EMAIL - SOME CHARACTER(S)
. '[A-Z0-9._-]*' // AN EMAIL - SOME CHARACTER(S) PERMITS DOT
. '@' // A SINGLE AT-SIGN
. '([A-Z0-9][A-Z0-9-]*\.)+' // A DOMAIN NAME PERMITS DOT, ENDS DOT
. '[A-Z\.]' // A TOP-LEVEL DOMAIN PERMITS DOT
. '{2,6}' // TLD LENGTH >= 2 AND =< 6
. '$' // ENDOF STRING
. '/' // ENDOF REGEX DELIMITER
. 'i' // CASE INSENSITIVE
;
// TEST THE STRING FORMAT
if (!preg_match($regex, $email))
{
$return[0] = FALSE;
$return[1] = "FAILED REGEX TEST";
return $return;
}
}
// TEST TO SEE IF THE DOMAIN IS IN OUR BLOCKED LIST
foreach ($bogus as $badguy)
{
if (stripos($email, $badguy))
{
$return[0] = FALSE;
$return[1] = "BLOCKED DOMAIN";
return $return;
}
}
// FILTER_VAR OR PREG_MATCH DOES NOT TEST IF THE DOMAIN IS ROUTABLE
$domain = explode('@', $email);
// MAN PAGE: http://php.net/manual/en/function.checkdnsrr.php
if ( checkdnsrr($domain[1], "MX") || checkdnsrr($domain[1], "A") )
{
$return[0] = TRUE;
$return[1] = NULL;
return $return;
}
// EMAIL IS NOT ROUTABLE
$return[0] = FALSE;
$return[1] = "NOT ROUTABLE";
return $return;
}
// DEMONSTRATE THE FUNCTION IN ACTION
$e = NULL;
if (!empty($_GET["e"]))
{
$e = $_GET["e"];
$r = check_valid_email($e);
if ($r[0])
{
echo "<br/>VALID: $e";
}
else
{
echo "<br/>BOGUS: $e";
echo "<br/>$r[1]";
}
}
// END OF PROCESSING - CREATE THE FORM USING HEREDOC NOTATION
$form = <<<ENDFORM
<form>
TEST A STRING FOR A VALID EMAIL ADDRESS:
<input name="e" value="$e" />
<input type="submit" />
</form>
ENDFORM;
echo $form;
Title | # Comments | Views | Activity |
---|---|---|---|
Multilanguage Database Design in MySQL | 5 | 33 | |
Convert a GMT TimeStamp to Local Time using PHP | 3 | 18 | |
Wordpress update causing pages to crash | 1 | 17 | |
Error installing extension Linux | 4 | 25 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
9 Experts available now in Live!