使用正则表达式验证密码 [英] Use regular expressions to validate passwords
本文介绍了使用正则表达式验证密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个密码字段,它应该符合以下条件
I have a password field which should match the following conditions
- 应该至少有一个字母和一个数字
- 最小长度为 5,最大长度为 20
另外,我想知道是否所有语言的正则表达式都相同?
Also, I would like to know if regular expressions are the same for all languages?
链接到开始使用正则表达式的优秀教程(假设我只有基本的了解)也很好.
Links to good tutorials to get started with Regular Expressions (assuming I have just a basic understanding) would also be nice.
谢谢
推荐答案
最小值和最大值只需使用 strlen代码>
.
The min and max just use strlen
.
我将使用的一个字符和一个数字 preg_match
:
The one character and one digit I would use preg_match
:
$len = strlen($string);
if ($len < 5) {
// too short
}elseif ( $len > 20) {
// too long.
}elseif (!preg_match('#[0-9]#', $string)) {
// does not contain a digit
}elseif (!preg_match('#[a-z]#i', $string)) {
// does not have a character
}
我喜欢将我的检查分成多个检查,以便我可以准确地告诉用户缺少什么.有些人更喜欢将其捆绑在一起.
I like to break mine out to multiple checks so I can tell the user exactly what is missing. Some people prefer it bundled into one.
这篇关于使用正则表达式验证密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文