Form validation help

How do I fix it so the the values are only invalid if they do not match the Regex?

<?php ini_set("display_errors", 1); // Turns on error reporting for this program. error_reporting(E_ALL | E_STRICT); //Show all possible problems to browser. $elements = array( //Lets us be able to get the names to be validated below (ch 7 - PHP)- suggested in project assignment 'selected_play' => "", //These are the names of the input elements from purchase.php. They are set to keys 'numberoftickets' => "", //The "=>" sets the values of the keys to empty 'first-name' => "", 'last-name' => "", 'address' => "", 'city' => "", 'state' => "", 'zip' => "", 'email' =>"", 'creditcard' => "", 'creditcard-number' => "", 'cvv-number' => ""); $invalidElements = array(); //Lets us store the results of each validity test. Puts all the failed input names and error messages by making the key and value pairs at the end of the stated array foreach (array_keys($elements) as $newKey) {//Lets us refer to the keys of $elements and gets those keys to become values of a new array in order to be put into the $_POST[$newKey] expression $value = trim($_POST[$newKey]);//Prevents data from having extraneous whitespaces and addresses potential hacking through some injection during POSTing if (!isset($value)) {//Determines if the $value variable is set and is not null - http://us3.php.net/manual/en/function.isset.php return true; } if($value ==""){//Determines whether the $value variable is actually empty. If the value is false or the variable doesn't exist then it is empty $invalidElements[$newKey] = $invalid_error_message;//This pushes input name ($newKey) into the $invalidElements array as a key and makes any specific error message ($invalid_error_message) as its value to . This enables us to track if there are any errors by checking whether or not $invalidElements is empty. preg_replace("/\-/", " ", $value); } switch ($newKey) { //Lets us to go through individual cases. Lets us compare the same variable with many different values and the Regex are determinants of the validity of the values. //Regex derived from the tools given in http://www.w3schools.com/jsref/jsref_obj_regexp.asp case 'first-name'://Got some insight from User Contributed Notes - Yousef Ismaeil Cliprz - http://us3.php.net/manual/en/function.preg-match.php if (!preg_match("[a-zA-Z]{3,30}", $value)) {//Searches subject (whatever is entered to the field) for a match to the regular expression pattern. $invalid_error_message = "First Name";//If it is not a match then the error message is printed (or echoed) which is initiated later on. } else { return true; } break; case 'last-name': if (!preg_match("[a-zA-Z]{3,30}", $value)) { $invalid_error_message = "Last Name"; } else { return true; } break; case 'address': if (!preg_match("d{1,3}\s[a-zA-Z]{2,30}\s[a-zA-Z]{2,10}", $value)) { $invalid_error_message = "Address"; } else { return true; } break; case 'city': if (!preg_match("[a-zA-Z]{3,30}", $value)) { $invalid_error_message = "City"; } else { return true; } break; case 'state': if (!preg_match("/^\w{2}$/'", $value)) { $invalid_error_message = "State"; } else { return true; } break; case 'zip': if (!preg_match("/^[0-9]{5}(?:-[0-9]{4})?$/", $value)) { $invalid_error_message = "Zip Code"; } else { return true; } break; case 'email': if (!preg_match("/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/",$value)) { $invalid_error_message = "Email"; } else { return true; } break; case 'creditcard-number': if (!preg_match("/(d{15,16})/", $value)) { $invalid_error_message = "Credit Card Number"; } else { return true; } break; case 'cvv-number': if (!preg_match("/(d{3,4})/", $value)) { $invalid_error_message = "CVV number"; } else { return true; } break; } $elements[$newKey] = $value;// Special step. There are values still hanging around for each iteration of the loop in the variable $value. So this makes the values of $newKey and $value in each iteration to go back and complete the original $elements array } ?>

This is a duplicate post. I covered this in another thread!

Sponsor our Newsletter | Privacy Policy | Terms of Service