Skip to content

Instantly share code, notes, and snippets.

@andr3a88
Created November 7, 2018 11:42
Show Gist options
  • Save andr3a88/ef8393f0602019b1983e154168888dfe to your computer and use it in GitHub Desktop.
Save andr3a88/ef8393f0602019b1983e154168888dfe to your computer and use it in GitHub Desktop.
Utils for password validation with different criterias
extension WTFViewModel {
let passwordMinChars: Int = 6
let passwordMinCriterias: Int = 3
/// Checks if the text meets the validity criteria
///
/// - Parameter text: The text
/// - Returns: The result
private func isPasswordCriteriaSatisfied(text: String) -> Bool {
let capitalCriteria = text.evaluate(regex: ".*[A-Z]+.*", predicateFormat: "SELF MATCHES %@")
let lowerCriteria = text.evaluate(regex: ".*[a-z]+.*", predicateFormat: "SELF MATCHES %@")
let numberCriteria = text.evaluate(regex: ".*[0-9]+.*", predicateFormat: "SELF MATCHES %@")
let specialCriteria = text.evaluate(regex: ".*[!&^%$#@()/.,+-:_=?\\[\\]]+.*", predicateFormat: "SELF MATCHES %@")
return [capitalCriteria, lowerCriteria, numberCriteria, specialCriteria].filter({ $0 }).count >= passwordMinCriterias
&& text.count >= passwordMinChars
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment