scm22ri Posted October 28, 2012 Share Posted October 28, 2012 Hi Everyone, I have a question regarding validating my form when a user inputs information. For "city" if a user inputs 333 or 23223 or any other type of number you'll get the error message "Your city must be valid". I just want to know if I'm going about the correct method for form validation? My goal is to make sure the user is non numerical data into the city field. I think I might be doing it correctly. What do you guys think? Thanks! <?php if (isset ( $_POST['name'], $_POST['name2'], $_POST['state'], $_POST['city'], $_POST['age'], $_POST['email'], $_POST['password'], $_POST['password2'] ) ){ $name = $_POST['name']; $name2 = $_POST['name2']; $state = $_POST['state']; $city = $_POST['city']; $age = $_POST['age']; $email = $_POST['email']; $password = $_POST['password']; $password2 = $_POST['password2']; // || empty($state) || empty($city) // how would I make sure a person that's inserting a city, enters a actual city and not a number? if ( empty($name) || empty($name2) || empty($age) || empty($city) || empty($email) || empty($password) || empty($password2) ){ // $name33 = "Fill in Name"; $errors[] = "All Fields Are Required! <br>"; } else { if (strlen($name AND $name2) > 25){ $errors[] = 'Name is too long. Shorten it'.'<br>'; } if ($name != $name2){ $errors[] = 'Names don\'t match. Write matching names!'.'<br>'; } if (is_numeric($city)){ $errors[] = 'Your city must be valid!'; } if (!is_numeric($age)){ $errors[] = 'Please enter a valid age.'.'<br>'; } else if ($age <=0){ $errors[] = 'Please enter a valid age.'.'<br>'; } if (filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){ $errors[] = 'Please enter a valid email.'.'<br>'; } if ($password != $password2){ $errors[] = 'Your passwords don\'t match'.'<br>'; } } // print_r ($errors); // echo "$whatever"; // echo "$name33"; if(!empty($errors)){ foreach ($errors as $error){ echo "$error"; } } else { echo "Register User!"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269990-validation-question/ Share on other sites More sharing options...
jonsjava Posted October 28, 2012 Share Posted October 28, 2012 As it stands, that's decent validation. If you wanted to get all fancy on it, you could take your error array, as well as the POST array, and push it to $_SESSION, redirect back to the form, and have the form pick up the values. If you go that route, you can set it so all error fields aren't re-populated in the form. This way, the customers won't lose any data other than the bad data. Quote Link to comment https://forums.phpfreaks.com/topic/269990-validation-question/#findComment-1388226 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.