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

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

问题描述

我需要一个正规的前pression与条件:

I need a regular expression with condition:


  • 分6个字符,最多50个字符

  • 必须包含1个字母

  • 必须包含1号

  • 可以包含特殊字符,如@#$%^&安培;!*()_ +

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

Currently I have pattern: (?!^[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使用来告诉是不是遇到什么PWD规则的用户:

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");
}

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

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