How to implement a regex for password validation in Swift?

前端 未结 6 1501
刺人心
刺人心 2020-12-22 16:18

I want to implement a regex validaton for passwords in Swift? I have tried the following regex, but not successful

([(0-9)(A-Z)(!@#$%ˆ&*+-=<>)]+)([         


        
6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-22 17:09

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

提交回复
热议问题