Search code examples
iosiphonevalidationnsnumberformatter

Validate user input number


In my app I just want to allow user to enter decimal number in "1111.11" format.That means upto 4 digit whole number and upto 2 digit decimal number I want to check this when user is editing in textbox. If user enter wrong input then is should not allow to enter into textbox.I have written code in:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

method but though I didnt get desired outcome. Here is my code

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (textField == txtFund)
{
    double num;
        long iPart;
        double fPart;
            num = [textField.text doubleValue];
            iPart = (long) num;
            fPart = num - iPart;
        NSLog(@"1 %ld %f",iPart,fPart);
        NSString *integerPart=[NSString stringWithFormat:@"%ld",iPart];

            if([integerPart length]>=4){
                if([string isEqualToString:@"."]){

                }
                else{
                return NO;
                }
            }
    NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

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

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

return YES;
 }

By this code I am able to achieve out put upto like "4444." but then it not allow Because it goes to else part in second if


Solution

  • This code should do want you want.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
    
        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
        NSString *expression = @"^([0-9]{1,4})?(\\.([0-9]{1,2})?)?$";
    
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:expression
                                                                               options:NSRegularExpressionCaseInsensitive
                                                                                 error:nil];
        NSUInteger numberOfMatches = [regex numberOfMatchesInString:newString
                                                            options:0
                                                              range:NSMakeRange(0, [newString length])];
        if (numberOfMatches == 0)
            return NO;
    
        return YES;
    }