I am trying to check the validity of a phone number that the user enters through a form. I think I have been able to come up with the code that checks it (I'm still pretty new to PHP), but I ONLY want to test it when the user enters their number -- not every time. I didn't want to force the user to enter their phone numbers. Any suggestions?
Below is my code:
<?php
// PHONE (AREA CODE) VALIDATION //
if(ereg("^[0-9]{3}$", $phoneac)) {
echo "You must enter a 3 digit area code.</p>";
}
// PHONE (NUMBER) VALIDATION //
elseif(ereg("^[0-9]{3}[-][0-9]{4}$", $phonen)) {
echo "You must enter your phone number in the following format: 111-1111</p>";
}
?>
Thanks for your help!