Frage

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

War es hilfreich?

Lösung

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;
}

Andere Tipps

Instead of user typing the . decimal symbol, you can insert one programmatically and allow the user to continue. Try the below code:

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //NSLog(@"%i", textField.text.length);

        NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
        for (int i = 0; i < [string length]; i++) {
            unichar c = [string characterAtIndex:i];
            if (![myCharSet characterIsMember:c])
            {
                return NO;
            }
            if(textField.text.length == 4) // Place a '.' when the text length is 4
            {
              textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@"."];
            }
            if(textField.text.length >=7) // Restrict the user to enter only 7 digits incl. the dot
            {
                return NO;
            }
        }
        return YES;
}

UPDATE: (If you really want the user to tyPe in the dot, then try the below code)

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    //NSLog(@"%i", textField.text.length);

        NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
        for (int i = 0; i < [string length]; i++) {
            unichar c = [string characterAtIndex:i];
            if (![myCharSet characterIsMember:c])
            {
                return NO;
            }
            if(textField.text.length == 4 && (![string isEqualToString:@"."])) // Checks for the 5th character and if it is not '.', then not letting the user to type in..
            {

                return NO;
             // textField.text = [textField.text stringByReplacingCharactersInRange:range withString:@"."];
            }
            if(textField.text.length >=7) // Restrict the user to enter only 7 digits incl. the dot
            {
                return NO;
            }
        }
        return YES;
}

You can check out using textfield textFieldShouldEndEditing method

 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
 {
    // getting the textfield text 
    // Separating the textfield value by (.)
    NSArray *arr=[textField.text componentsSeparatedByString:@"."];
    // Checking the textfiled text before dot
    NSLog(@"arr lenth @0==%i",[[arr objectAtIndex:0] length]);
    // Checking the textfiled text after dot
    NSLog(@"arr lenth @1==%i",[[arr objectAtIndex:1] length]);

   if([[arr objectAtIndex:0] length]>4)
   {
    // Comes here if textfield text value before dot is greater then 4 digits
    }

     if([[arr objectAtIndex:1] length]>2)
   {
      // Comes here if textfield text value after dot is greater then 2 digits
    }
 }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top