Page 1 of 1

PHP validation - phone number or e-mail required

Posted: Wed Feb 27, 2008 5:49 am
by jovankau
Hi,

Just wondering if anyone has some PHP code for a web form that I am creating. Looking to ensure that the user supplies either a contact phone number or e-mail address. Hope someone is willing to share some code. Also, should I place this code after the existing phone number and e-mail validation code?

Thanks in advance,
Jo

Re: PHP validation - phone number or e-mail required

Posted: Wed Feb 27, 2008 10:59 am
by Christopher
Typically you would check each variable of interest to see if it exists and contains a value. If it does not then set an error variable. If when you are done with you checks there are no errors then redirect to a success page, otherwise redisplay the form with error messages.

Re: PHP validation - phone number or e-mail required

Posted: Wed Feb 27, 2008 11:51 am
by ccrevling
what do you have for the script now?? i want to help.

Re: PHP validation - phone number or e-mail required

Posted: Wed Feb 27, 2008 5:35 pm
by jovankau
Hi ccrevling,

What I have so far are 2 separate scripts one to check for valid e-mail and one to check for valid phone number. Here they are:

Code: Select all

 
 
// Validate email field.
 
if(isset($_REQUEST['email']) && !empty($_REQUEST['email']))
 
{
 
if(preg_match("/(%0A|%0D|\n+|\r+|:)/i",$_REQUEST['email'])){$errors[] = "ERROR
 
"
 
//Bunch of HTML code to make error screen look nice
 
;}
 
$_REQUEST['email'] = trim($_REQUEST['email']);
 
if(substr_count($_REQUEST['email'],"@") != 1 || stristr($_REQUEST['email']," ")){$errors[] = "ERROR
 
"
 
// Bunch of HTML code to make error screen look nice
 
;}
 
else{$exploded_email = explode("@",$_REQUEST['email']);
 
if (empty($exploded_email[0]) || strlen($exploded_email[0]) > 64 || empty($exploded_email[1])){$errors[] = "ERROR
 
"
// Bunch of HTML code to make error screen look nice
 
;}
 
else{ if (substr_count($exploded_email[1],".") == 0){$errors[] = "ERROR
"
 
// Bunch of HTML code to make error screen look nice
 
;}
 
else{$exploded_domain = explode(".",$exploded_email[1]);
 
if (in_array("",$exploded_domain)){$errors[] = "ERROR
 
"
 
// Bunch of HTML code to make error screen look nice
 
;}
 
else { foreach($exploded_domain as $value){if(strlen($value) > 63 || !preg_match('/^[a-z0-9-]+$/i',$value)){$errors[] = "ERROR
"
 
// Bunch of HTML code to make error screen look nice
  
; break;}}}}}
 
}}
 
// Check referrer is from same site.
 
if(!(isset($_SERVER['HTTP_REFERER']) && !empty($_SERVER['HTTP_REFERER']) && stristr($_SERVER['HTTP_REFERER'],$_SERVER['HTTP_HOST']))){$errors[] = "ERROR";}
 
 
// Check for valid phone number.
 
// Checks if the user has supplied a non empty phone number
if (isset($_REQUEST['phone']) && !empty($_REQUEST['phone'])) 
 
    // String without spaces 
{
    $phoneNumber = trim($_REQUEST['phone']);
 
    // Regex to make sure the phone number is all digits incl =-()."  " and 2 to 15 characters long
 
    if (ereg("[0-9\.\9\(\)\-\_\+\' '\]{2,15}$", $phoneNumber) == false) {
 
    // Append to the error list if the regex failed
        $errors[] = "ERROR
"
        
// Bunch of HTML code to make error screen look nice
 
;}}   
unset($set);
 
 
Phew, well there it is. If you could help with some code to then check that there is either a phone number or an e-mail address provided by the user, it would be greatly appreaciated

Many thanks,
Jo

Re: PHP validation - phone number or e-mail required

Posted: Wed Feb 27, 2008 7:03 pm
by Christopher
It would be easier to test your code if you did not split it up. Comments in the code would be better.

Re: PHP validation - phone number or e-mail required

Posted: Wed Feb 27, 2008 7:10 pm
by jovankau
Hi Chris,

Have made the change you suggested. Sorry, beginners mistake.

Jo

Re: PHP validation - phone number or e-mail required

Posted: Thu Feb 28, 2008 4:54 pm
by jovankau
Hi again,

What I have so far for cross-field validation is:

Code: Select all

 
 
if (empty('phone') && (!empty('email'))) {
 
return false; 
}
 
 
However, I can't seem to get this to work.

Just to show you here are the terms I have used in the code as well:

$phoneNumber = trim($_REQUEST['phone']);

and

$_REQUEST['email'] = trim($_REQUEST['email']);

What am I doing wrong in the above vailidation?

Many thanks,
Jo