Skip to content

Instantly share code, notes, and snippets.

View naknut's full-sized avatar

Marcus Isaksson naknut

  • Mpya Digital
  • Stockholm, Sweden
  • X @naknut
View GitHub Profile
@naknut
naknut / numberValidation.swift
Created January 31, 2018 13:35
Number validation
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
let newText = ((textField.text as NSString?) ?? "").replacingCharacters(in: range, with: string)
guard newText.count > 0 else {
return true
}
guard (newText.filter{ $0 == Character(Locale.current.decimalSeparator!) } as [Character]).count <= 1 else { // Make sure we only have one decimal separator
return false
}
let numbersSet: Set<Character> = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
var withoutSeperator = newText

Keybase proof

I hereby claim:

  • I am naknut on github.
  • I am naknut (https://keybase.io/naknut) on keybase.
  • I have a public key ASDaT3bujEItW5cUOFqFrp5DqHcOxGRFnMCuB-nl7ZomVAo

To claim this, I am signing this object: