Create preg_match for password validation allowing

2019-01-13 16:39发布

I would like to create a preg_match function to validate my passowrds, but I'm not sure how to write it to allow the following special characters to be used: !@#$%.

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

Here are my password rules that I want to work into the regex:

  • May contain letter 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条回答
我想做一个坏孩纸
2楼-- · 2019-01-13 17:17
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 thus exist?.

查看更多
疯言疯语
3楼-- · 2019-01-13 17:25
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.';
}
查看更多
地球回转人心会变
4楼-- · 2019-01-13 17:26

I've done this with my drupal custom 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.'));
}
?>
查看更多
我只想做你的唯一
5楼-- · 2019-01-13 17:35

I have developed a complete regex for a bit more complex check

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

Basically I check for the password to have 1 digit, 1 capital, 1 lower and 1 special character. I hope this helps someone looking for a regex.

查看更多
家丑人穷心不美
6楼-- · 2019-01-13 17:35

I think this should look like that:

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

Between start -> ^
And end -> $
of the string there has to be at least one number -> (?=.*\d)
and at least one letter -> (?=.*[A-Za-z])
and it has to be a number, a letter or one of the following: !@#$% -> [0-9A-Za-z!@#$%]
and there have to be 8-12 characters -> {8,12}

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

btw, you might want to take a look at this regex tutorial

查看更多
聊天终结者
7楼-- · 2019-01-13 17:42

I liked r3bel's answer, so I had a play with it and ended up with the following as a password-checking function:

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 .= '(?=.*[a-z])'; }           // Match at least 1 lowercase letter
    if ($req_upper == 1) { $regex .= '(?=.*[A-Z])'; }           // 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;
    }
}

Max/Min lengths are default or adjustable, each requirement is default on, but can be switched off, and I wanted to support any symbols so the last requirement is "anything that isn't one of the above types", rather than a fixed set of symbols.

查看更多
登录 后发表回答