我想在Swift中实现一个密码的正则表达式验证?我尝试了下面的正则表达式,但没有成功。
([(0-9)(A-Z)(!@#$%ˆ&*+-=<>)]+)([a-z]*){6,15}
我的要求如下:密码必须超过6个字符,并且至少包含一个大写字母、数字或特殊字符。
我想在Swift中实现一个密码的正则表达式验证?我尝试了下面的正则表达式,但没有成功。
([(0-9)(A-Z)(!@#$%ˆ&*+-=<>)]+)([a-z]*){6,15}
我的要求如下:密码必须超过6个字符,并且至少包含一个大写字母、数字或特殊字符。
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
^ Start anchor
(?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
(?=.*[!@#$&*]) Ensure string has one special case letter.
(?=.*[0-9].*[0-9]) Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8} Ensure string is of length 8.
$ End anchor.
Source - Rublar Link
尝试使用此代码,以确保密码必须超过6个字符,并且至少包含一个大写字母、数字或特殊字符
^.*(?=.{6,})(?=.*[A-Z])(?=.*[a-zA-Z])(?=.*\\d)|(?=.*[!#$%&? "]).*$
^ assert position at start of the string
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
(?=.{6,}) Positive Lookahead - Assert that the regex below can be matched
.{6,} matches any character (except newline)
Quantifier: {6,} Between 6 and unlimited times, as many times as possible, giving back as needed [greedy]
(?=.*[A-Z]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[A-Z] match a single character present in the list below
A-Z a single character in the range between A and Z (case sensitive)
(?=.*[a-zA-Z]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[a-zA-Z] match a single character present in the list below
a-z a single character in the range between a and z (case sensitive)
A-Z a single character in the range between A and Z (case sensitive)
(?=.*\\d) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
\d match a digit [0-9]
2nd Alternative: (?=.*[!#$%&? "]).*$
(?=.*[!#$%&? "]) Positive Lookahead - Assert that the regex below can be matched
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
[!#$%&? "] match a single character present in the list below
!#$%&? " a single character in the list !#$%&? " literally (case sensitive)
.* matches any character (except newline)
Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
$ assert position at end of the string
https://regex101.com/#javascript
可以尝试更多内容...
至少8个字符,至少1个字母和1个数字:
"^(?=.*[A-Za-z])(?=.*\\d)[A-Za-z\\d]{8,}$"
至少8个字符,包括1个字母、1个数字和1个特殊字符:
"^(?=.*[A-Za-z])(?=.*\\d)(?=.*[$@$!%*#?&])[A-Za-z\\d$@$!%*#?&]{8,}$"
至少8个字符,其中包括至少1个大写字母、1个小写字母和1个数字:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)[a-zA-Z\\d]{8,}$"
至少8个字符,其中包括1个大写字母、1个小写字母、1个数字和1个特殊字符:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[d$@$!%*?&#])[A-Za-z\\dd$@$!%*?&#]{8,}"
最少8个字符,最多10个字符,至少包含1个大写字母、1个小写字母、1个数字和1个特殊字符:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[$@$!%*?&#])[A-Za-z\\d$@$!%*?&#]{8,10}"
public func isValidPassword() -> Bool {
let passwordRegex = "^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])[0-9a-zA-Z!@#$%^&*()\\-_=+{}|?>.<,:;~`’]{8,}$"
return NSPredicate(format: "SELF MATCHES %@", passwordRegex).evaluate(with: self)
}
如果您需要快速解决方案,这是一个使用正则表达式进行密码验证的示例。将其复制/粘贴到助手或扩展文件中并使用。func isValidPassword() -> Bool {
// least one uppercase,
// least one digit
// least one lowercase
// least one symbol
// min 8 characters total
let password = self.trimmingCharacters(in: CharacterSet.whitespaces)
let passwordRegx = "^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&<>*~:`-]).{8,}$"
let passwordCheck = NSPredicate(format: "SELF MATCHES %@",passwordRegx)
return passwordCheck.evaluate(with: password)
}
缺少验证的原因:
func getMissingValidation(str: String) -> [String] {
var errors: [String] = []
if(!NSPredicate(format:"SELF MATCHES %@", ".*[A-Z]+.*").evaluate(with: str)){
errors.append("least one uppercase")
}
if(!NSPredicate(format:"SELF MATCHES %@", ".*[0-9]+.*").evaluate(with: str)){
errors.append("least one digit")
}
if(!NSPredicate(format:"SELF MATCHES %@", ".*[!&^%$#@()/]+.*").evaluate(with: str)){
errors.append("least one symbol")
}
if(!NSPredicate(format:"SELF MATCHES %@", ".*[a-z]+.*").evaluate(with: str)){
errors.append("least one lowercase")
}
if(str.count < 8){
errors.append("min 8 characters total")
}
return errors
}
getMissingValidation
函数。 - Yuyutsu正则表达式是
(?:(?:(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_])|(?:(?=.*?[0-9])|(?=.*?[A-Z])|(?=.*?[-!@#$%&*ˆ+=_])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_]))[A-Za-z0-9-!@#$%&*ˆ+=_]{6,15}
func validpassword(mypassword: String) -> Bool { //定义密码规则 let passwordreg = ("(?=.*[A-Z])(?=.*[0-9])(?=.*[a-z])(?=.*[@#$%^&*]).{8,}") //使用正则表达式测试密码是否符合规则 let passwordtesting = NSPredicate(format: "SELF MATCHES %@", passwordreg) return passwordtesting.evaluate(with: mypassword) }
//创建一个密码文本框IBOutlet @IBOutlet weak var password_textfield: UITextField!
//单击并调用登录按钮 @IBAction func login_button(_ sender: UIButton) { //获取文本框数据并通过函数检查 let password = validpassword(mypassword: password_textfield.text!) if(password == false) { print("有效密码") //用于警报消息框 } else { print("登录安全") //用于警报消息框 } }
## 函数用于验证密码和确认密码是否相同,密码必须超过一定的字符数,密码包含某些特殊字符,密码必须有一个数字,密码必须有一个大写字母 ##