-1

I have a text field which the user enters a number and it gets converted to an integer.

The keyboard set is to decimal numerical one. Except the paste function is still enabled so the user can paste text into it.

I want to set it to change a label to @"error" if the user enters a non numerical value into it.

I do not want to disable the paste function as I still want the user to have the ability to copy and paste their own numerical values into my app.

mspensieri
  • 3,501
  • 15
  • 18
  • possible duplicate of [iPhone how to check that a string is numeric only](http://stackoverflow.com/questions/1320295/iphone-how-to-check-that-a-string-is-numeric-only) – Pratik Bhiyani Apr 24 '14 at 04:28

3 Answers3

3

If you want apply restriction that use have to only enter numeric value then use delegate method of UITextField:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{ 
   NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
   return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) || [string isEqualToString:@""];
}  

This will returns YES if the string is numeric, otherwise return NO.

EDITE:

@Adam's answer is the best for check text is only numeric or not.

iPatel
  • 46,010
  • 16
  • 115
  • 137
  • I don't understand where to put this, and what do I change to make it relate back to the respective text field? – user3567176 Apr 24 '14 at 04:32
  • @user3567176 - just read http://www.roseindia.net/tutorial/iphone/examples/iphone-iPhone-Text-Field-Validation.html – iPatel Apr 24 '14 at 04:37
  • Still lost, I'm sorry I'm very new to coding. I don't know how to do this! – user3567176 Apr 24 '14 at 04:42
  • Declare UITextFieldDelegate in .h file and then declare textValue.delegate = self; into viewDidLoad method, in the .m file and then use this above method – iPatel Apr 24 '14 at 04:47
  • `stringByTrimmingCharactersInSet:` only removes none numeric characters from the begin and end of the string, so for example `0000aaa000` will not be altered and is this valid although it does contain non numeric values. This is a possibility when the user pastes the string into the text field. – rckoenes Apr 24 '14 at 14:56
  • Okay so I did [self textField:termEntered shouldChangeCharactersInRange:WHATS HERE replacementString:@"0"] I dont know what to put into my character range. Please help – user3567176 Apr 24 '14 at 23:37
  • @rckoenes - yah.. but you are saying that using `stringByTrimmingCharactersInSet:` **only removes none numeric characters from the begin and end of the string** OK then this method is call for each char that you are entering so `"stringByTrimmingCharactersInSet"` is check for each char that you are enter, so it is not any possibility to enter alphabet. I tried with this code in my demo project and it's working very well for me . Thanks :) – iPatel Apr 25 '14 at 04:35
  • @iPatel yes when you are typing yes, when you past a string into it it will not work. – rckoenes Apr 25 '14 at 06:50
3

Simply disallowing entry of non-numeric characters is one way. But what you asked is how do you detect non-numerics...

Use

NSRange range = [myTextField.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
if(range.location == NSNotFound) {
    // then it is numeric only
}
Adam Eberbach
  • 12,309
  • 6
  • 62
  • 114
  • Beautiful. Very concise and readable and a great implementation of the standard lib. – Brian Tracy Apr 24 '14 at 04:12
  • So I include label.text = @"error" within the if statement right? – user3567176 Apr 24 '14 at 04:14
  • it says "expected identifier" after [[NSCharacterSet] – user3567176 Apr 24 '14 at 04:15
  • Yes, that would work. I'm not sure it's the greatest user experience though. For one you are confusing them by asking for non-numerics only, though you are displaying non-numerics in the same place. I'd think about a popup label asking for numerics only - not a modal dialog, just a hint, easily visible near the text field. (there was a typo in that code - but it is fixed now) – Adam Eberbach Apr 24 '14 at 04:16
  • Okay how would I dissalow entry of non-numeric chartaceters? – user3567176 Apr 24 '14 at 04:21
  • 1
    for that you want iPatel's answer - return NO from shouldChangeCharactersInRange if non-numeric characters are encountered. You could just use the code I provided, in that function, and return YES where it says "is numeric only" or NO otherwise. – Adam Eberbach Apr 24 '14 at 04:23
0
NSRange    range    =    [myTextField.text    rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
Aman
  • 51
  • 5