Skip to content

Instantly share code, notes, and snippets.

@gr4y
Created April 14, 2012 20:05
Show Gist options
  • Select an option

  • Save gr4y/2387672 to your computer and use it in GitHub Desktop.

Select an option

Save gr4y/2387672 to your computer and use it in GitHub Desktop.
allow only numbers in UITextField. For now without currency!
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789."] invertedSet];
BOOL result = NO;
if([string length] == 0){ //backspace
result = YES;
}
else{
if([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0){
result = YES;
}
}
return result;
}
@CanTheAlmighty
Copy link

Only works with US numbers tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment