EDIT: I don't need the regex for the phone number, I have that already. What I need is help understanding how to insert that into my existing code, which I linked above. If you actually read my question, you'd see that I'm trying to learn what I need to modify in my original piece of code, not just copy and paste someone else's regex without knowing what to do with it. Thanks! EDIT x2: I figured it out.. Thanks anyway. (See answer below)
I have a piece of code that works to make sure something is in the phone field:
if (!empty($_POST['hphone'])) {$hphone = $_POST['hphone'];} else {$hphone = NULL; echo '<span class="error">Please enter a home phone number.</span><br />';}
In order to be more precise, I figured I'd try and make it check that it's a valid phone number. I found an example and tried to combine them:
if (!empty($_POST['hphone']) && !preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone)) {$hphone = $_POST['hphone'];} else {$hphone = NULL; echo '<span class="error">Please enter a home phone number.</span><br />';}
This works in that the form still submits but it doesn't catch that there's a letter in the field. It just submits. I modified it some more to where it says there's not a valid number even though it is. I'm not a PHP guru so I'm just trying to do what makes sense to me with what I've got to work and other people's examples. Sorry for yet another post about this but I couldn't find exactly what I was after. Thanks!
ANSWER: I ended up figuring it out a different way. Below is the code that worked for me:
if (preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $_POST['hphone'])) { $hphone = $_POST['hphone']; } else { $hphone = NULL; echo '<span class="error">invalid</span>'; }