密码 REGEX,最少 6 个字符,至少一个字母和一个数字,可能包含特殊字符 [英] Password REGEX with min 6 chars, at least one letter and one number and may contain special characters

查看:39
本文介绍了密码 REGEX,最少 6 个字符,至少一个字母和一个数字,可能包含特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个带条件的正则表达式:

I need a regular expression with condition:

  • 最少 6 个字符,最多 50 个字符
  • 必须包含 1 个字母
  • 必须包含 1 个数字
  • 可能包含特殊字符,如 !@#$%^&*()_+

目前我有模式:(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,50})$

但是它不允许特殊字符,有没有人有一个好的正则表达式?

However it doesn't allow special characters, does anybody have a good regex for that?

谢谢

推荐答案

也许可以使用单个正则表达式,但这使得很难向用户反馈他们没有遵循哪个规则.像这样的更传统的方法为您提供反馈,您可以在 UI 中使用这些反馈来告诉用户哪些密码规则没有得到满足:

Perhaps a single regex could be used, but that makes it hard to give the user feedback for which rule they aren't following. A more traditional approach like this gives you feedback that you can use in the UI to tell the user what pwd rule is not being met:

function checkPwd(str) {
    if (str.length < 6) {
        return("too_short");
    } else if (str.length > 50) {
        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");
}

这篇关于密码 REGEX,最少 6 个字符,至少一个字母和一个数字,可能包含特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆