ios 如何在 Swift 中实现用于密码验证的正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39284607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to implement a regex for password validation in Swift?
提问by Vaibhav Jhaveri
I want to implement a regex validaton for passwords in Swift? I have tried the following regex, but not successful
我想在 Swift 中为密码实现正则表达式验证吗?我尝试了以下正则表达式,但没有成功
([(0-9)(A-Z)(!@#$%?&*+-=<>)]+)([a-z]*){6,15}
My requirement is as follows: Password must be more than 6 characters, with at least one capital, numeric or special character
我的要求如下: 密码必须大于 6 个字符,至少有一个大写、数字或特殊字符
采纳答案by Vaibhav Jhaveri
The regex is
正则表达式是
(?:(?:(?=.*?[0-9])(?=.*?[-!@#$%&*?+=_])|(?:(?=.*?[0-9])|(?=.*?[A-Z])|(?=.*?[-!@#$%&*?+=_])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%&*?+=_]))[A-Za-z0-9-!@#$%&*?+=_]{6,15}
回答by Anand Nimje
You can use Regex for check your passwordstrength
您可以使用正则表达式检查您的密码强度
^(?=.*[A-Z].*[A-Z])(?=.*[!@#$&*])(?=.*[0-9].*[0-9])(?=.*[a-z].*[a-z].*[a-z]).{8}$
Regex Explanation : -
正则表达式解释:-
^ 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
来源- Rublar 链接
回答by Nazmul Hasan
try with this one for Password must be more than 6 characters, with at least one capital, numeric or special character
试试这个密码必须超过6个字符,至少有一个大写、数字或特殊字符
^.*(?=.{6,})(?=.*[A-Z])(?=.*[a-zA-Z])(?=.*\\d)|(?=.*[!#$%&? "]).*$
^.*(?=.{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
https://regex101.com/#javascript
more this you can try ....
更多这个你可以试试....
Minimum 8 characters at least 1 Alphabet and 1 Number:
最少 8 个字符,至少 1 个字母和 1 个数字:
"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"
Minimum 8 characters at least 1 Alphabet, 1 Number and 1 Special Character:
最少 8 个字符,至少 1 个字母、1 个数字和 1 个特殊字符:
"^(?=.*[A-Za-z])(?=.*\d)(?=.*[$@$!%*#?&])[A-Za-z\d$@$!%*#?&]{8,}$"
Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet and 1 Number:
最少 8 个字符,至少 1 个大写字母、1 个小写字母和 1 个数字:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$"
Minimum 8 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
最少 8 个字符,至少 1 个大写字母、1 个小写字母、1 个数字和 1 个特殊字符:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[d$@$!%*?&#])[A-Za-z\dd$@$!%*?&#]{8,}"
Minimum 8 and Maximum 10 characters at least 1 Uppercase Alphabet, 1 Lowercase Alphabet, 1 Number and 1 Special Character:
最少 8 个和最多 10 个字符,至少 1 个大写字母、1 个小写字母、1 个数字和 1 个特殊字符:
"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&#])[A-Za-z\d$@$!%*?&#]{8,10}"
回答by tBug
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)
}
If you need a quick fix. This is validation for a password with regex. Copy/paste in helper or extension file and use it.
如果您需要快速修复。这是使用正则表达式验证密码。复制/粘贴帮助程序或扩展文件并使用它。
回答by PRAVEEN BHATI
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 weak var password_textfield: UITextField! //create a Password text field IBOutlet @IBAction func login_btton(_ sender: UIButton) { //Click & Call to Login Button let password = validpassword(mypassword: password_textfield.text!) //get text Field data & checked through the function if(password == false) { print("Valid Password") //Use to Alert Msg Box } else { print("Login Safe") //Use to Alert Msg Box } } ## Function Use to validation is password and confirm password is same, Password must have more then some characters , Password contain some special character , Password must one digit , Password must one uppercase letter ##
回答by Yuyutsu
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)
}