0

I just heard about this regex and am trying to get it to work.

My code:

func isPasswordValid(_ Password : String) -> Bool{
    let Password = NSPredicate(format: "SELF MATCHES %@", "^(?=.*[A-Z](?=.*[0-9].{>8}$")
    return Password.evaluate(with: Password)
}

@IBAction func Register(_ sender: UIButton) {
    if Username.text == "" {
        Error.text = "Email må ikke være tom";
    }
    if Username.text != "" {
        Error.text = ""
    }
    if Password.text == "" {
        Error2.text = "Password må ikke være tom"
    }
    if Password.text != "" {
        Error2.text = ""
    }
    if Password.text != repeatPassword.text || repeatPassword.text == "" {
        Error3.text = "Password skal være det samme"
    }
    if Password.text == repeatPassword.text && repeatPassword.text != "" {
        Error3.text = ""
    }
    if Password.text! == repeatPassword.text && Username.text != "" {
        Auth.auth().createUser(withEmail: Username.text!, password: Password.text!) { (User, Error) in
            self.performSegue(withIdentifier: "goToSignIn", sender: self)
        }
    }

}

when I run it it makes a breakpoint at the return state. I guess Im needing something? Can anyone help?

1
  • What is the regex / validation supposed to do? Please conform to the naming convention that variable names start with a lowercase letter. Commented Sep 28, 2017 at 13:19

3 Answers 3

2
func isPasswordValid(_ Password : String) -> Bool{
    let Password = NSPredicate(format: "SELF MATCHES %@", "^(?=.*[A-Z](?=.*[0-9].{>8}$")
    return Password.evaluate(with: Password)
}

I suspect your function parameter is conflicting with the predicate variable name, try this:

func isPasswordValid(_ password : String) -> Bool{
    let predicate = NSPredicate(format: "SELF MATCHES %@", "^(?=.*[A-Z](?=.*[0-9].{>8}$")
    return predicate.evaluate(with: password)
}

@IBAction func Register(_ sender: UIButton) {

    let username = self.username.text.flatMap { $0.isEmpty ? nil : $0 }
    let password = self.password.text.flatMap { $0.isEmpty ? nil : $0 }
    let repeat = self.repeatPassword.text.flatMap { $0 == password }

    error.text = username == nil ? "Email må ikke være tom" : ""
    error2.text = password == nil ? "Password må ikke være tom" : ""
    error3.text = repeatPassword == nil ? "Password skal være det samme" : ""

    guard username != nil, repeat != nil else { return }

    Auth.auth().createUser(withEmail: username!, password: password!) { (user, error) in
        self.performSegue(withIdentifier: "goToSignIn", sender: self)
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

It looks like it is working now. However if you look in my last lines of code. In the if statement im making sure that all is filled and the password is the same. How should I write this to make it confirm that aswell?
@JonasLarsen I have updated my answer to update the register method
1

.{>8} is not valid in a regex, use instead:

let Password = NSPredicate(format: "SELF MATCHES %@", "^(?=.*[A-Z])(?=.*[0-9]).{8,}$")
#                                               close lookahead __^       __^ ^^^^^

3 Comments

Oh okay, so now its at least 8 characters?
@JonasLarsen: Yes it is.
Perfect, now I just need to clarify that it is true before I run the last if statement, any ideas?
0

Swift 5

func isValidPassword(_ password:String) -> Bool {
   if(password.count > 7 && password.count < 17) {
   } else {
       return false
   }
   let nonUpperCase = CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZ").inverted
   let letters = password.components(separatedBy: nonUpperCase)
   let strUpper: String = letters.joined()

   let smallLetterRegEx  = ".*[a-z]+.*"
   let samlltest = NSPredicate(format:"SELF MATCHES %@", smallLetterRegEx)
   let smallresult = samlltest.evaluate(with: password)

   let numberRegEx  = ".*[0-9]+.*"
   let numbertest = NSPredicate(format:"SELF MATCHES %@", numberRegEx)
   let numberresult = numbertest.evaluate(with: password)

   let regex = try! NSRegularExpression(pattern: ".*[^A-Za-z0-9].*", options: NSRegularExpression.Options())
   var isSpecial :Bool = false
   if regex.firstMatch(in: password, options: NSRegularExpression.MatchingOptions(), range:NSMakeRange(0, password.count)) != nil {
    print("could not handle special characters")
       isSpecial = true
   }else{
       isSpecial = false
   }
   return (strUpper.count >= 1) && smallresult && numberresult && isSpecial
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.