-
-
Save kenegozi/38efab4ef3a810efb0d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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