Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
private func validateInputs() -> Bool {
let nonEmpty = {
(data:String!) -> Bool in
return count(data) != 0
}
func inRange (min: Int!, max:Int!) -> (String!) -> Bool {
return { (data:String!) -> Bool in
let dataAsInt = data.toInt()
return (dataAsInt! >= min && dataAsInt! <= max)
}
}
var inputs : [UITextField: (String, [String! -> Bool] )] = [
cpfField!: ("Ops, enter your id", [nonEmpty]),
pwdField!: ("enter your password", [nonEmpty]),
dobDayField!: ("invalid day", [nonEmpty, inRange(1, 31)]),
dobMonthField!: ("Invalid month", [nonEmpty, inRange(1, 12)]),
dobYearField!: ("Invalid year", [nonEmpty, inRange(1900, 2010)]),
]
func applyValidationRules(inputs:[UITextField: (String, [String! -> Bool] )]) -> Bool {
for entry in inputs {
let input = entry.0
let (errorMessage, rules) = entry.1
for rule in rules {
if (!rule(input.text)) {
showError(errorMessage)
input.becomeFirstResponder()
return false
}
}
}
return true
}
return applyValidationRules(inputs)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.