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:
- Check IF anything was inputted at all. 2.If it WAS, follow the above.
- 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