Let's pretend the following:
<?php
// Functions to validate/sanitize user input
function validateUsername() {
// If accepted, return true, else return false
}
function validatePassword() {
// If accepted, return true, else return false
}
function validateEmail() {
// If accepted, return true, else return false
}
This is how I handle user input:
// Getting all user input
$values = $_POST['values'];
$error = false;
if (!validateUsername($username) && $error === false) {
$errorMessage = "Username can't contain special characters";
$error = true;
}
if (!validatePassword($password) && $error === false) {
$errorMessage = "Password is'nt secure enought";
$error = true;
}
if (!validateEmail($email) && $error === false) {
$errorMessage = "Email is not correctly formatted";
$error = true;
}
if ($error === true) {
echo $errorMessage;
} else {
// Do something
}
?>
But I'm sure there is a better approach. What is the best (or a good) way to handle user input messages/errors?
Also I've read a tutorial (which unfortunately I can't find anymore) where it was recommended to give the users hints and tips about their input, instead of giving them a big bold red warning.
For example to accept not only 1234 AB
(Dutch postcodes), but also 1234ab
, 1234AB
and 1234 ab
and let the script convert it to the official notation 1234 AB
.