The following PHP function gets an email address and validate it in according to the requirment
and send a String message to the calling program.
function validateEmailAddress($email) // This php function validate an E-mail address for
// 1. E-mail address is not empty
// 2. An at symbol @,
// 3. One or more characters before the @,
// 4. and a dot somewhere after @.
// This function returns a string message to the calling program
{
$email = trim($email); // clear any extra spaces in front and end of email address
$past = 0;
$valid = false;
$dot = false;
$message = NULL;
if(strlen($email) > 0) // email is not empty
{
if(strstr($email, "@")) // find @ symbol in email
{
for($counter=0; $counter < strlen($email); $counter++)
{
if($email[$counter] == "@")
{
$prior= $counter +1;
$past = 0;
$dot = false;
continue;
}
if($email[$counter] == "." && $past > 0)
$dot = true;
$past++;
} // end for
if($prior > 0 && $past > 0 && $dot == true)
$valid = true;
} // end if
if ($valid == false)
{
$message = 'Your Email address does not seem right.';
}
}
else
{
$message = 'You forgot to enter your Email address.';
}
return $message;
}// end function
The below PHP function is almost identical to above function only it gets an email address
validate it in according to the requirment and send a BOOLEAN VALUE to the calling program.
function validateEmail($email) // This php function validate an E-mail address for
// 1. E-mail address is not empty
// 2. An at symbol @,
// 3. One or more characters before the @,
// 4. and a dot somewhere after @.
// This function returns a boolean value to the calling program
{
$email = trim($email);
$past = 0;
$valid = false;
$dot = false;
$message = true;
The following PHP function validate an URL for correct format and return an String Message
to the Calling program.
function validateURL($url) // This function check if an URL is in correct format. It assumed URL must
// start with an http:// prefix and at least one dot.
{
$url = trim($url);
$valid = false;
$message = NULL;
if(strlen($url) > 0)
{
if(strstr($url, "http://"))
{
if($url[0]== "h" && $url[1]=="t")
{
if(strlen($url) > 12)
{
if(strstr($url, "."))
$valid = true;
}// end if
} // end if
} // end if
}// end if
else
{
$valid = true;
}
if($valid == false)
{
$message = 'Your Website address does not seem right.';
}
return $message;
}// end function