djcrisp22 Posted October 7, 2008 Share Posted October 7, 2008 how can i tell in my script in the phone number box to look for only numbers and same thing with zip code? any help is appreciated! <?php if (!isset($_POST['submitForm'])) { ?> <form action="index.php" method="POST"> <table> <tr><td>First Name:</td><td><input type="text" name="name"></td></tr> <tr><td>Last Name:</td><td><input type="text" name="lastname"></td></tr> <tr><td>Address:</td><td><input type="text" name="address"></td></tr> <tr><td>City:</td><td><input type="text" name="city"></td></tr> <tr><td>State:</td><td><input type="text" name="state"></td></tr> <tr><td>Zip:</td><td><input type="text" name="zip"></td></tr> <tr><td>Phone Number:</td><td><input type="text" name="phone"></td></tr> <tr><td>Email:</td><td><input type="text" name="email"></td></tr> <tr><td><input type="submit" name="SubmitForm" value="Send"></td></tr> <form> <?php } else {echo "Form submitted!";} ?> <?php // Function to display form function showForm($errorName=false,$errorEmail=false,$errorAddress=false,$errorCity=false,$errorLastName,$errorState,$errorZip,$errorPhone){ if ($errorName) $errorTextName = "Please enter your first name!"; if ($errorLastName) $errorTextLastName = "Please enter your last name!"; if ($errorCity) $errorTextCity = "Please enter a city!"; if ($errorEmail) $errorTextEmail = "Please enter a valid email address!"; if ($errorAddress) $errorTextAddress = "Please enter a address!"; if ($errorState) $errorTextState = "Please enter name of state!"; if ($errorZip) $errorTextZip = "Please enter zip code!"; if ($errorPhone) $errorTextPhone = "Please enter phone number!"; echo '<form action="index.php" method="POST"><table>'; // Display firstname field an error if needed echo '<tr><td>First Name:</td><td><input type="text" name="name"></td></tr>'; if ($errorName) echo "<tr><td colspan='2'>$errorTextName</td></tr>"; // Display lastname field an error if needed echo '<tr><td>Last Name:</td><td><input type="text" name="lastname"></td></tr>'; if ($errorLastName) echo "<tr><td colspan='2'>$errorTextLastName</td></tr>"; //display address field an error if needed echo ' <tr><td>Address:</td><td><input type="text" name="address"></td></tr>'; if ($errorAddress) echo "<tr><td colspan='2'>$errorTextAddress</td></tr>"; // Display City field an error if needed echo '<tr><td>City:</td><td><input type="text" name="city"></td></tr>'; if ($errorCity) echo "<tr><td colspan='2'>$errorTextCity</td></tr>"; // Display State field an error if needed echo '<tr><td>State:</td><td><input type="text" name="state"></td></tr>'; if ($errorState) echo "<tr><td colspan='2'>$errorTextState</td></tr>"; // Display zip field an error if needed echo '<tr><td>Zip:</td><td><input type="text" name="zip"></td></tr>'; if ($errorZip) echo "<tr><td colspan='2'>$errorTextZip</td></tr>"; // Display phone number field an error if needed echo '<tr><td>Phone Number:</td><td><input type="text" name="phone"></td></tr>'; if ($errorPhone) echo "<tr><td colspan='2'>$errorTextPhone</td></tr>"; // Display email field an error if needed echo '<tr><td>Email:</td><td><input type="text" name="email"></td></tr>'; if ($errorEmail) echo "<tr><td colspan='2'>$errorTextEmail</td></tr>"; echo '<tr><td><input type="submit" name="SubmitForm" value="Send"></td></tr>'; echo '<form>'; } if (!isset($_POST['SubmitForm'])) { showForm(); } else { //Init error variables $errorName = false; $errorLastName = false; $errorAddress = false; $errorCity = false; $errorState = false; $errorZip = false; $errorPhone = false; $errorEmail = false; $name = isset($_POST['name']) ? trim($_POST['name']) : ''; $lastname = isset($_POST['lastname']) ? trim($_POST['lastname']) : ''; $address = isset($_POST['address']) ? trim($_POST['address']) : ''; $city = isset($_POST['city']) ? trim($_POST['city']) : ''; $state = isset($_POST['state']) ? trim($_POST['state']) : ''; $zip = isset($_POST['zip']) ? trim($_POST['zip']) : ''; $phone = isset($_POST['phone']) ? trim($_POST['phone']) : ''; $email = isset($_POST['email']) ? trim($_POST['email']) : ''; if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) $errorEmail = true; if (strlen($name)<3) $errorName = true; if (strlen($lastname)<3) $errorLastName = true; if (strlen($address)<1) $errorAddress = true; if (strlen($city)<1) $errorCity = true; if (strlen($state)<1) $errorState = true; if (strlen($zip)<1) $errorZip = true; if (strlen($phone)<1) $errorPhone = true; // Display the form again as there was an error if ($errorName || $errorEmail || $errorAddress || $errorCity || $errorLastName || $errorState || $errorZip || $errorPhone) { showForm($errorName,$errorEmail,$errorAddress,$errorCity,$errorLastName,$errorState,$errorZip,$errorPhone); } else { echo "submission success"; } } ?> Link to comment https://forums.phpfreaks.com/topic/127343-phone-number-validation-and-zip-code-validation/ Share on other sites More sharing options...
R0bb0b Posted October 7, 2008 Share Posted October 7, 2008 regular expression, just like you did the email. Link to comment https://forums.phpfreaks.com/topic/127343-phone-number-validation-and-zip-code-validation/#findComment-658757 Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 if(ereg('\(?[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}', $phone)) { // phone number isn't valid } if(ereg('\b[0-9]{5}(?:-[0-9]{4})?\b', $zip)) { // zip code isn't valid } That *should* work. Link to comment https://forums.phpfreaks.com/topic/127343-phone-number-validation-and-zip-code-validation/#findComment-658758 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.