Domanda

Im trying to apply validation on UITextField in such a way that it should not accept anything rather than decimal number. I used following delegate method of UITextField:

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


     NSDecimalNumber *number2 = [NSDecimalNumber decimalNumberWithString:string];
    if (!string || [string length] < 1 || [string isEqualToString:@""] )
    {
        return YES;
    }
    if (!number2 || [number2 isEqualToNumber:[NSDecimalNumber notANumber]])
    {
        return NO;
    }

    return YES;
}

Its working perfect,but it has two issues.

1. when i'm entering value 'E' or 'e' its accepting it.

2.Once it accepted letter 'E' or 'e',Its accepting all other english letters.

È stato utile?

Soluzione

This is from the documentation.

Besides digits, numericString can include an initial “+” or “–”; a single “E” or “e”, to indicate the exponent of a number in scientific notation; and a single NSLocaleDecimalSeparator to divide the fractional from the integral part of the number.

Maybe you can use NSRegularExpression instead

Altri suggerimenti

You should use NSNumberFormatter to validate numeric user input.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top