Create a preg_match to verify the password allowing (! @ # $%)

I would like to create a preg_match function to check my passports, but I'm not sure how to write it to use the following special characters !@#$% .

 if(!preg_match(?????)$/', $password)) 

Here are my password rules that I want to use in regex:

  • May contain letters and numbers
  • Must contain at least 1 number and 1 letter
  • May contain any of these characters !@#$%
  • Must be 8-12 characters

Thank you for any help you can offer.

+8
php regex passwords preg-match
source share
8 answers

I think it should look like this:

 if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $password)) { echo 'the password does not meet the requirements!'; } 

Between the beginning โ†’ ^
And the end โ†’ $
strings must have at least one number โ†’ (?=.*\d)
and at least one letter โ†’ (?=.*[A-Za-z])
and it must be a number, a letter or one of the following :! @ # $% โ†’ [0-9A-Za-z!@#$%]
and should be 8-12 characters โ†’ {8,12}

As user557846 commented on your question, I would also suggest you allow more characters, I usually (if I use the maximum) take at least 50 :)

btw, you can take a look at this regex tutorial

+42
source share
 preg_match('/^(?=.*\d)(?=.*[@#\-_$%^&+=ยง!\?])(?=.*[az])(?=.*[AZ])[0-9A-Za-z@#\-_$%^&+=ยง!\?]{8,20}$/',$password) 
  • at least one lowercase char
  • at least one uppercase char
  • at least one digit
  • at least one special character @ # -_ $% ^ & + = ยง !?
+6
source share

I liked the answer of r3bel, so I had a game with it, and as a password verification function, I got the following:

 function password_strength_check($password, $min_len = 8, $max_len = 70, $req_digit = 1, $req_lower = 1, $req_upper = 1, $req_symbol = 1) { // Build regex string depending on requirements for the password $regex = '/^'; if ($req_digit == 1) { $regex .= '(?=.*\d)'; } // Match at least 1 digit if ($req_lower == 1) { $regex .= '(?=.*[az])'; } // Match at least 1 lowercase letter if ($req_upper == 1) { $regex .= '(?=.*[AZ])'; } // Match at least 1 uppercase letter if ($req_symbol == 1) { $regex .= '(?=.*[^a-zA-Z\d])'; } // Match at least 1 character that is none of the above $regex .= '.{' . $min_len . ',' . $max_len . '}$/'; if(preg_match($regex, $password)) { return TRUE; } else { return FALSE; } } 

The maximum / minimum length is by default or configurable, each requirement is enabled by default, but can be disabled, and I would like to support any characters, so the last requirement is "all that is not one of the above types", but a fixed character set.

+2
source share

I designed a full regex for more complex validation

 /^(?=.*\d)(?=.*[A-Za-z])(?=.*[AZ])(?=.*[az])(?=.*[ !#$%&'\(\) * +,-.\/[\\] ^ _`{|}~\"])[0-9A-Za-z !#$%&'\(\) * +,-.\/[\\] ^ _`{|}~\"]{8,50}$/ 

Basically, I check that the password has 1 digit, 1 capital, 1 lower and 1 special character. Hope this helps someone search for regex.

+1
source share

I did this with my special drupal form in hook_form_validate here the password should be 6 characters of letters, numbers and at least one special character.

 <? if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])(?=.*[!@#$%])[0-9A-Za-z!@#$%]{6,15}$/', $form_state['values']['pass'])) { form_set_error('pass', t('Password must contain 6 characters of letters, numbers and at least one special character.')); } ?> 
0
source share
 if (!preg_match('/^(?=.*\d)(?=.*[A-Za-z])(?=.*[!@#$%])[0-9A-Za-z!@#$%] {6,15}$/',($_POST['password']))) { $message='Password must contain 6 characters of letters, numbers and at least one special character.'; } 
0
source share

Search for character classes, the main function of regular expressions.

-one
source share
 if(!preg_match('/^(?=.*\d)(?=.*[A-Za-z])[0-9A-Za-z!@#$%]{8,12}$/', $password)) { echo 'the password does not meet the requirements!'; } 

In the above statement .. what possible password exists?

-one
source share

All Articles