Validate Phone Number

A

Anonymous

Guest
I have a form with and if elseif statement and I'm trying to validate the phone number and eventually the Social Securtiy number first by determining if the field is empty then is there are actual numbers. I was trying the phone number first and when I test the form the php gets stuck on validating the phone number. My code looks like this:

if ($this->fname == ""){$this->error = 1;}
elseif ($this->lname == ""){$this->error = 2;}
elseif ($this->ssNum == ""){$this->error = 3;}
elseif ($this->addr1 == ""){$this->error = 4;}
elseif ($this->city == ""){$this->error = 5;}
elseif ($this->state == ""){$this->error = 6;}
elseif ($this->zip == ""){$this->error = 7;}
elseif ($this->phone == ""){$this->error = 8;}
elseif ($this->phone != "('/^\d{3,3}-?\d{3,3}-?\d{4,4}$/', $phone)"){$this->error = 8;}

How should I handle the phone number validation in this example.

Thanks,
Rob
 
Why not using Javascript for that !?
Code:
/* JavaScript code */

function isPhoneNumber(s) {
 
     // Check for correct phone number
     rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);
 
     if (!rePhoneNumber.test(s)) {
          alert("Phone Number Must Be Entered As: (555) 555-1234");
          return false;
     }
 
return true;

}
Code:
<?php

return ereg("^([0-9]{3})[0-9]{3}-[0-9]{4}$", $number) ? true : false;

?>
Social Security Number Check:
Code:
<?php

// Depending on the format
return ereg("^[0-9]{9}", $number) ? true : false;

?>
 
Back
Top