4
Good community!
I have the following doubt. How can I alert the user that there are blanks in the field username
?
This is how I’m validating username
now:
if (empty($_POST["username"])) {
$nameErr = "Escolha um username.";
} else {
$uname = test_input($_POST["username"]);
$v1='ok';
if (!preg_match("/^[a-zA-Z-0-9-_]*$/",$uname)) {
$nameErr = "Somente letras e números.";
}
}
The solution was this:
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$nameErr = "Escolha um username.";
} else {
$uname = test_input($_POST["username"]);
// check if name only contains letters and whitespace
// aqui
if (!ereg("(^[a-zA-Z0-9]+([a-zA-Z\_0-9\.-]*))$", $_POST["username"]) ) {
$nameErr = "Somente letras e números.";
}
}
Form validation? Have you ever considered using javascript? This type of server-side validation only wastes request and processing.
– user28595
Thank you for replying @Diegofelipe. Yes is registration form validation. In this case, the field "username". I do not want spaces in username... I’m by the same php
– David Concha
@Diego Felipe, The server-side validation should always be performed independent of client-side validation.
– Daniel Omine
Modify your question, @David Concha, because the way you’re different from what you said. A blank field is very different from removing spaces in a specific field.
– Daniel Omine
@Diegofelipe validation is only on the server side. On the client side you can only do a simple preliminary check, but there is no way to avoid sending inappropriate data.
– Bacco
@Danielomine yes, but having validation on the client’s side already avoids many requests simply because one field or another has not been filled in. I see php more to confirm validation(user can disable javascript) or as security that data is actually being passed correctly.
– user28595
@Bacco but you do not agree that it greatly reduces the number of wasted requests because of validation that could be alerted to the user before submitting?
– user28595
@Diegofelipe I do not agree with the part that you said that "this validation only wastes request and processing", because it is fundamental for data integrity is always that of the server. As band savings and requisition, I agree that pre-validation is good (that part I didn’t even question, incidentally).
– Bacco