Last active
January 9, 2016 23:31
-
-
Save jayjansheski/01392e4da9037c2868d4 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
func stringIsValidDecimalFormat(input: String) -> Bool { | |
let validDecimalCharacters = "0123456789.-" | |
for character in input.characters { | |
let characterAsString = String(character) | |
if validDecimalCharacters.rangeOfString(characterAsString) == nil { | |
return false | |
} else if characterAsString == "." && (input.componentsSeparatedByString(".").count) > 2 { | |
return false | |
} | |
} | |
return true | |
} | |
func textField(textField: UITextField, | |
shouldChangeCharactersInRange range: NSRange, | |
replacementString newString: String) -> Bool { | |
if stringIsValidDecimalFormat(textField.text! + newString) == false { | |
return false | |
} else { | |
return true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment