Skip to content

Instantly share code, notes, and snippets.

@hammett
Created May 31, 2015 05:09
Show Gist options
  • Save hammett/7a0f1190b3664ec151ff to your computer and use it in GitHub Desktop.
Save hammett/7a0f1190b3664ec151ff to your computer and use it in GitHub Desktop.
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 yoru 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