Password max length - php

I have a password validation script for the signup process and it works OK if I don't set a maximum length limit. If I add a maximum limit of 32 it ignores the validation, creating the account, even if I have more than 32 characters.
Working script:
if (preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["password"]) === 0)
{
echo "The password must contain lower case characters, upper case characters and numbers. It's length should be between 8 and 32 characters.";
}
Not working script:
if (preg_match("/^.*(?=.{8,32})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["password"]) === 0)
{
echo "The password must contain lower case characters, upper case characters and numbers. It's length should be between 8 and 32 characters.";
}
Please let me know before downrating so that I can edit my question. Thanks!

To limit the length, just edit your regular expression. You're already half way there.
/^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).{8,32}$/
Demo: http://regex101.com/r/nK5yY6

Doing it in one big regex is a bit of a code maintenance nightmare. Splitting it up is far easier to figure out for someone else looking at your code, and it allows you to give more specific error messages as well.
This solution works for me , try it :
$password = $_POST["password"];
$uppercase = preg_match('#[A-Z]#', $password);
$lowercase = preg_match('#[a-z]#', $password);
$number = preg_match('#[0-9]#', $password);
$length = preg_match("#^.{8,32}$#" , $password);
if(!$uppercase || !$lowercase || !$number || !$length ) {
echo "The password must contain lower case characters, upper case characters and numbers. It's length should be between 8 and 32 characters.";
}

if (preg_match("/^.*(?=.{8,})(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z]).*$/", $_POST["password"]) === 0 || strlen($_POST['password']) < 8 || strlen($_POST['password']) > 32 )
{
echo "The password must contain lower case characters, upper case characters and numbers. It's length should be between 8 and 32 characters.";
}

If you are looking for password with atleast 1 digit and 1 alphabet please try this:
^.*(?=.{8,32})(?=.*\d)(?=.*[a-zA-Z]).*$

Related

How to validate both greater than AND less than

I am writing validation for a book management system, and I want to make it so that if the length of the ISBN entered is either less than or greater than 13 or 10, it displays the error message, however it says that there is an unexpected '<' whenever I try the following.
if(strlen($_POST['isbndelete'] (< 13 || 10) || (> 13 || 10)))
{
$error="The length of the ISBN is incorrect.";
echo $error;
return false;
}
All help is appreciated!
Your main issue is that your condition is not valid PHP. You should read more about conditional statements syntax.
Validing ISBN length
ISBNs are either 10 or 13 characters.
So you can simply check if your string does not contain exactly 10 and does not contain 13 characters either, like this:
$len = strlen($_POST['isbndelete']);
if ($len != 10 && $len != 13) {
$error = "The length of the ISBN is incorrect.";
echo $error;
return false;
}

Validating lengh of a phone with PHP

I'm trying to validate the length of a phone number staying within a range. Let's say at least 9 characters but not more than 12 so I can get international phone numbers.
I tried several things but none of them work.
The option below for instance validates correctly that it has not letter, however it doesn't matter the length of the number I introduce, I always get the Error Message: "Your phone number needs to have 9-11 numbers" even if I introduce a 9, 10 or 11 eleven digits number.
Thank you so much
if (empty($_POST["cellphone"])) {
$cellphoneErr = "Cell Phone is required";
} else {
$cellphone = test_input($_POST["cellphone"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[0-9]*$/",$cellphone)) {
$cellphoneErr = "Only numbers allow";
}
elseif(strlen($_POST["cellphone"] < 9) || strlen($_POST["cellphone"] > 11)){
$cellphoneErr = "Your phone number needs to have 9-11 numbers";
}
}
Use preg_match() with quantifier {min,max}:
if (!preg_match("/^[0-9]{9,11}$/",$cellphone)) {
$cellphoneErr = "Has to be 9 to 11 numbers.";
}
elseif(strlen($_POST["cellphone"] < 9) || strlen($_POST["cellphone"] > 11)){
Should be:
elseif(strlen($_POST["cellphone"]) < 9 || strlen($_POST["cellphone"]) > 11){
Your parenthesis are wrong.

PHP password validate number 12345 or abcde using regex

For my rule here I want to validate a password field that contains at least 5 characters long but those must not be abcde or 12345 or reverse. How can I? Here I don't want to force users to enter at least 1 letter 1 number or a symbols in combination.
$uppercase = preg_match('#[A-Z]#', $password);
$lowercase = preg_match('#[a-z]#', $password);
$number = preg_match('#[0-9]#', $password);
if(!$uppercase || !$lowercase || !$number || strlen($password) < 8) {
// tell the user something went wrong
}
documentation: http://php.net/manual/de/function.preg-match.php
source: Regex for password PHP

Multiple conditions in one statement

I'm adding some very basic validation to a "name" form field. Generally, it's impossible to actually validate a name, but I figured I could at least verify that it's not empty, greater than maybe 2 characters (Al is the shortest name I can think of), and that those characters aren't just empty space.
Here's the conditionals I'm using:
// Check length of name field
if(!isset($name) || $name < 2 || (strlen($name) > 0 && strlen(trim($name)) == 0)) {
// Name field only spaces
if((strlen($name) > 0 && strlen(trim($name)) == 0) || trim($name) == '') {
$errors['name'] = "Please enter a real name...";
}
// Name too short
else {
$errors['name'] = "Are you sure <strong>".htmlspecialchars($name)."</strong> is your name?";
}
$msg_type = "error";
}
However, when I run this with a valid name, I get the "Name too short" error. I know it's got to be a problem with how I'm combining the conditionals, but I can't figure out where that problem lies.
$name < 2 doesn't work. You're trying to use strlen($name) < 2.
Well, there is a tool called regex which people have invented for string matching and it could be pretty conveniently used for validation cases like yours. If you want to validate a word let's say with at least 2 characters of length, you could do the following:
if(!preg_match('/\b\w{2,}/', $name)) {
$errors['name'] = "Are you sure <strong>".htmlspecialchars($name)."</strong> is your name?";
}
Where:
\b: word boundary
\w: word character
{2,}: two or more times for the word character

Simple 'Greater Than' form validation

I am trying to validate my registration form, and one of my validations doesn't work. I want to echo a error message if the first name is over 20 characters long. I am using the code
} else if($_POST["mem_first_name"] >20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
However no error is shown if more than 20 characters are entered. However if I use the same code but change it to less than like this
} else if($_POST["mem_first_name"] <20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
Then it works, is there a simple fix?
if(strlen($_POST["mem_first_name"]) > 20)
Use strlen() function
} else if(strlen($_POST["mem_first_name"]) >20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
How about using the strlen() function, for counting strings? It is stable and appropriate way of counting number of characters IMO
} else if(strlen($_POST["mem_first_name"]) >20) {
$errors[] = 'Sorry but your First name is limited to 20 Characters each';
}
You have to use strlen() function for string length checks. Also, be aware that if you need multibyte encoding support, you should switch to using mb_strlen().
var_dump(strlen('bär')); // int(4) - byte length
var_dump(mb_strlen('bär', 'utf8')); // int(3) - character length

Categories