Hello Coders,
It’s always a need to validate email at user end. Not only this but strong password is also suggested to secure accounts.
Here are snippets to validate if correct email and strong password are entered or not.
//Validating email
func isValidEmail(email:String)->Bool{
let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,}"
let emailTest = NSPredicate(format:"SELF MATCHES %@", emailRegEx)
return emailTest.evaluate(with: email)
}
Below function will check for the password according to the regex, it’s required to have following condition for strong password
1- Minimum 8 characters to 24 characters max, you can change the limit as passed in arguments(make sure it's at least 4).
2- It should have atleast one capital letter.
3- password should have at least one numeric character.
4- there must be at least one special character.
//Validating password
func isValidPassword(password:String, minChar:Int = 8, maxChar:Int = 24)->Bool{
let passwordRegEx = "^(?=.*[A-Z])(?=.*[!@#$&*])(?=.*[0-9])(?=.*[a-z]).{\(minChar),\(maxChar)}$"
let passwordTest = NSPredicate(format:"SELF MATCHES %@", passwordRegEx)
return passwordTest.evaluate(with: password)
}
Now you can use above methods and their values accordingly. :)
0 Comment(s)