Search This Blog

Thursday, 28 February 2013

Number validation-iOS

Hello there,
         Here is the simple thing how to put validation for numbers only in xcode.This example is for contacts ,and will also include '+' ,in validation.


-(BOOL)numberValidation:(NSString *)number{
    NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet];
    BOOL stringIsValid = ([[number stringByTrimmingCharactersInSet:decimalSet] isEqualToString:@""] ||
                          [[number stringByTrimmingCharactersInSet:decimalSet] isEqualToString:@"+"]);
    return stringIsValid;

Example for how to use:

Suppose you have a UITextField IBOutlet 'abc'

so do it like this:

BOOL check = [self numberValidation:abc.text];


//show alert view ,when validation fails.


Happy Programming ;)

No comments:

Post a Comment