9

I am fairly new to iPhone development and I have what seems to be a simple question that I cannont figure out. How can I verify that a user input a number and a decimal into the text field?

I have tried many different things but the closes I can get only allows for numeric strings of the form: 5.34 or 23.89. I need it to allow numbers such as; 0.50 or 432.30, just something with a zero as the last input value. I attempted to check the string input by converting it to a float and then back to a string, but get the abovementioned results.

ANY help would be great (same with sample code!) THANKS!

3 Answers3

13

I had a similar requirement and the solution ultimately turned out to be fairly trivial. Unfortunately a lot of the questions and answers related to this question are about validating or formatting numeric values, not controlling what a user could input.

The following implementation of the shouldChangeCharactersInRange delegate method is my solution. As always, RegularExpressions rock in this situation. RegExLib.com is an excellent source for useful RegEx's. I'm not a RegEx guru and always struggle a bit putting them together so any suggestions to improve it are welcome.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (textField == self.quantityTextField)
    {
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

        NSString *expression = @"^([0-9]+)?(\\.([0-9]{1,2})?)?$";
        NSError *error = nil;

        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression 
                                                                               options:NSRegularExpressionCaseInsensitive 
                                                                                 error:&error];
        NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                            options:0
                                                              range:NSMakeRange(0, [newString length])];        
        if (numberOfMatches == 0)
            return NO;        
    }

    return YES;
}

The above code allows the user to input these kinds of values: 1, 1.1, 1.11, .1, .11

Seamus
  • 3,191
  • 3
  • 22
  • 20
1

The question that Dave DeLong was trying to post is 1320295 and looks very relevant. This appears to be an old question, youve probably found your solution, Would be nice if you share it will all =)

Community
  • 1
  • 1
Yarek T
  • 9,715
  • 2
  • 28
  • 38
1

You can cast the content of your textfield in float and format it by using the code below :

float myFloat = [myTextField floatValue];
NSString *formatString = [NSString stringWithFormat:@"%%1.%if", 2]; //2 decimals after point
NSString *resultString = [NSString stringWithFormat:formatString, myFloat];

If you want to allow only numeric values in your textfield, you can just reput the resultString in your textField so any not allowed text will be replaced by the formatted float value.

If the user puts "abc12.4" the result will be "0". So it would be better if you use the UITextFieldDelegate method :

textField:shouldChangeCharactersInRange:replacementString:

to check the last key tapped by user. You can just compare it to know if it's a numeric value or a point/comma.

Can
  • 91
  • 1
  • 5