Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenegozi/38efab4ef3a810efb0d8 to your computer and use it in GitHub Desktop.
Save kenegozi/38efab4ef3a810efb0d8 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 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