如何在Swift中实现密码验证的正则表达式?

时间:2016-09-02 05:02:10

标签: ios swift regex validation

我想在Swift中为密码实现正则表达式验证?我试过以下正则表达式,但没有成功

([(0-9)(A-Z)(!@#$%ˆ&*+-=<>)]+)([a-z]*){6,15}

我的要求如下:密码必须超过6个字符,至少有一个大写,数字或特殊字符

6 个答案:

答案 0 :(得分:52)

您可以使用Regex检查密码强度

^(?=.*[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.
  

来源 - Rublar Link

答案 1 :(得分:35)

尝试使用密码必须超过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}"

答案 2 :(得分:13)

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

如果您需要快速修复。这是使用正则表达式验证密码。 复制/粘贴帮助器或扩展文件并使用它。

答案 3 :(得分:6)

正则表达式是

(?:(?:(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_])|(?:(?=.*?[0-9])|(?=.*?[A-Z])|(?=.*?[-!@#$%&*ˆ+=_])))|(?=.*?[a-z])(?=.*?[0-9])(?=.*?[-!@#$%&*ˆ+=_]))[A-Za-z0-9-!@#$%&*ˆ+=_]{6,15}

答案 4 :(得分:2)

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)

}

答案 5 :(得分:1)

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 ##