Am working on a signup form which has an input for username, gender, email and password. I have successfully added validations am pleased with to the 3 except for the username input which am facing a single problem. I will like to apply a validation to my username input which checks if input has at least 3 letters (upper or lowercase) e.g.
I want the results be something like this:
Input=abc
count=3
result=accept!
--------------
Input=XYZQW
count=5
result=accept!
--------------
Input=A/_B
count=2
result=Not acceptable!
But now it counts everything such as /
or _
or (
or etc... as letters
below is my php code which i have applied few validations
<?php
$username = mysqli_real_escape_string($db, $_POST['username']);
if (empty($username)) {
$error_class = 'input_error';
} elseif (strlen($username) < 3) {
$error= '<font color="red">Your user name must be atleast 3 characters</font>';
$error_class = 'input_error';
} elseif (strlen($username) > 15) {
$error= '<font color="red">Your user name is too long</font>';
$error_class = 'input_error';
} elseif (!preg_match("/^[A-Za-z0-9_\.]+$/ ", $username)) {
$error = '<font color="red">Your user name must be in letters with either a number, underscore or a dot</font>';
$error_class = 'input_error';
} else {
$check_uname = "SELECT * FROM users WHERE username = '$username'";
if (!$result = mysqli_query($db_var, $check_uname)) {
exit(mysqli_error($db_var));
}
if (mysqli_num_rows($result) > 0) {
$error = '<font color="red"><b>'.$username.'</b> is already in use</font>';
$error_class = 'input_error';
} else {
$error_class = 'input_no_error';
}
}
?>