I have many textField Inputs for the calculations I want that if any of the inputs is not a number and negative number then it show error message on button click and not to move to the other screen
Asked
Active
Viewed 341 times
2 Answers
2
There's at least a couple ways you can handle this, James.
#1) specify numeric keyboards for those specific numeric-only text fields in the storyboard / xib file
#2) set a delegate for the text field and when the user is done editing (e.g. textFieldDidEndEditing:
), look at the contents of the text field and if you see anything that isn't a number, throw up a UIAlert.
One way to do this would be:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSRange rangeOfInvalidCharacter = [textField.text rangeOfCharacterFromSet: [[NSCharacterSet decimalDigitCharacterSet] invertedSet]];
if(rangeOfInvalidCharacter.location != NSNotFound)
{
// throw up a UIAlert here
// and, if you want, erase the bogus text via:
textField.text = @"";
return;
}
// and if we get here, that means the text field contents are only digits.
}

Community
- 1
- 1

Michael Dautermann
- 88,797
- 17
- 166
- 215
-
as you have described i don't want to switch to specific keboard second method is ok but i want to apply that on UIButton but how to check that it is a number or negative number – james May 31 '12 at 04:29
-
thanks i want to ask you a simple question that when we show alert for wrinog value then when we presss ok or cancell buton it closes alert box the wrong values is still in the textfield so it means we have to make two checks one in this method and one in other button where if wrong values is also enterd it moves to next screen or how we make that if there is wrong values entered then you can not move to the next view – james May 31 '12 at 04:41
-
That shouldn't be too hard for you to figure out how to solve it. Myself, I'd keep track of the text field that brought up the UIAlert and then take the user back there to that text field (make it the focus again) or remove the bogus text from the field. – Michael Dautermann May 31 '12 at 04:43
-
1how to remove the bogus text or make the textbox focus again and also your code is giving error expected : – james May 31 '12 at 04:45
-
Yep... I needed to add a pair of brackets. I've fixed this code example. To remove the bogus text, just do "`textField.text = @"";`" somewhere. – Michael Dautermann May 31 '12 at 04:48
-
i am doing like this but my app crashes when i do this – james May 31 '12 at 04:49
-
let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11968/discussion-between-james-and-michael-dautermann) – james May 31 '12 at 05:11
0
Try this code
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
{
NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789"] invertedSet];
if ([string rangeOfCharacterFromSet:set].location != NSNotFound) {
return NO; // For don't add this character
}
return YES; // For input a true character
}

CReaTuS
- 2,593
- 1
- 19
- 31
-
yes i know this method but how to check that the input is number or negative number – james May 31 '12 at 04:32