Regex to validate passwords with characters restrictions

后端 未结 3 941
渐次进展
渐次进展 2021-01-13 18:53

I need to validate a password with these rules:

  • 6 to 20 characters
  • Must contain at least one digit;
  • Must contain at least one letter (case in
3条回答
  •  一个人的身影
    2021-01-13 19:40

    Regex could be:-

    ^(?=.*\d)(?=.*[a-zA-Z])[a-zA-Z0-9!@#$%&*]{6,20}$
    

    How about this in Javascript:-

    function checkPwd(str) {
        if (str.length < 6) {
            return("too_short");
        } else if (str.length > 20) {
            return("too_long");
        } else if (str.search(/\d/) == -1) {
            return("no_num");
        } else if (str.search(/[a-zA-Z]/) == -1) {
            return("no_letter");
        } else if (str.search(/[^a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\_\+]/) != -1) {
            return("bad_char");
        }
        return("ok");
    }
    

    Also check out this

提交回复
热议问题