仅在输入信息时才进行表单验证

仅在输入信息时才进行表单验证

问题描述:

I have some client-side form validation for an email address. Essentially it checks if the email is too short, then if it's too long and finally uses FILTER_VALIDATE_EMAIL to check if it's valid.

However an adjustment has been made to the form. The email field is no longer required. But I don't just want to remove all validation as I still want to ensure that if an email address is input it is correct. It's just not a required field any more.

Here are the nested if statements for the validation at present:

if (isset($_POST['email_address'])){
    $email_address = mysql_real_escape_string(trim($_POST['email_address']));
    $_SESSION['status']['register']['email_address'] = $email_address;
    if(strlen($email_address) > 10){ // email address less than 10
        if(strlen($email_address) < 161){ // if longer than 160
            if(email_valid($email_address) == false){ // email address invalid format
                $_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
            }else{ 
                // passed min length, passed max length, passed validation
            }
        }else {
            $_SESSION['status']['register']['error'][] = 'The email address is too long.';
        }
    } else{
        $_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
    }
}else{
    $_SESSION['status']['register']['error'][] = "You haven't put in an email address.";
}

But I need to change this to do the following instead, but am having trouble getting my head around the nested statements:

  1. Check IF anything was inputted at all. 2.If it WAS, follow the above.
  2. If it wasn't, progress anyway.

So only if they actually input anything does the validation run. Else it's still OK to go forward with the form check because it wasn't required anyway. The full code can be seen here pastebin.com

change isset to !empty, and remove the else like suggested earlier.

if(!empty($_POST['email_address']) {
//do stuff
}

If you remove the final else you'll be good to go.

I believe that's how it should be if I am not mistaking.

if (!empty($_POST['email_address'])) {
    $email_address = mysql_real_escape_string(trim($_POST['email_address']));
    $_SESSION['status']['register']['email_address'] = $email_address;
    if(strlen($email_address) > 10){ // email address less than 10
        if(strlen($email_address) < 161){ // if longer than 160
            if(email_valid($email_address) == false){ // email address invalid format
                $_SESSION['status']['register']['error'][] = "The email address has been put in wrong. Please check and try again.";
            } else {
                // passed min length, passed max length, passed validation
            }
        } else { 
            $_SESSION['status']['register']['error'][] = 'The email address is too long.';
        }
    } else {
        $_SESSION['status']['register']['error'][] = "The email address is too short. It can't be shorter than 10 letters.";
    }
}

maybe you could try this it is much friendlier to use and easier to validate: When inserting into database please use PDO or mySQLi

<?php
function output_errors($error) {
    echo '<ul>';
    foreach($error as $err) {
        echo '<li>'.$err.'</li>';
    }
    echo '</ul>';
}

$email = mysql_real_escape_string(strip_tags($_POST['email_address']));
if(!empty($email)) {
    if(strlen($email) > 10) {
        $error[] = 'The email address is too short. It can\'t be shorter than 10 letters.';
    }

    if(strlen($email) < 161) {
     $error[] = 'The email address is too long.';
    }

    if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error[] = 'The email address has been put in wrong. Please check and try again.';
    }

    if(!empty($error)) {
        echo output_errors($error);
    } else {
        //do something
    }
}
?>