Jump to content

Validation Question?


scm22ri

Recommended Posts

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!";
}
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.