Search code examples
swift2ios9

Swift 2 iOS9 text case check


I'm trying to find a solution for the following example. I have created a user registration page and I check that the password reset question does not match the actual password (works okay), but what I want to do is convert the inputted password and password reset question to say lower case and check there is no match. I would prefer to do this on the fly rather that put the password into a var.

The reason for doing this is to ensure that the password reset question is not the same as the actual password regardless capitalisation of specific chars.

    if (passWord.text) == (resetQuestion.text) {
        let alertController = UIAlertController(title: "PASSWORD SECURITY ISSUE", message:
            "password reset question must not match actual password!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        return
    }


    if String.lowercaseString.rangeOfString(passWord.text) == String.lowercaseString.rangeOfString(resetQuestion.text) {
        let alertController = UIAlertController(title: "PASSWORD SECURITY ISSUE", message: 
            "password reset question must not match actual password!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        return
    }

Any pointers or help would be appreciated.


Solution

  • There is a caseInsensitiveCompare method that you could try. Something like

    if passWord.text.caseInsensitiveCompare(resetQuestion.text) == .OrderedSame {
            let alertController = UIAlertController(title: "PASSWORD SECURITY ISSUE", message:
            "password reset question must not match actual password!", preferredStyle: UIAlertControllerStyle.Alert)
        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
        return
    }