2

How to use regular expression in iOS sdk?

I need to validate UITextField using regular expression allow only numerical values into UITextField.

Piyush Dubey
  • 2,416
  • 1
  • 24
  • 39
user1478583
  • 342
  • 2
  • 7
  • 1
    [What have you tried](http://www.whathaveyoutried.com) – Ravi Kumar Karunanithi Jun 26 '12 at 11:17
  • @RKK - Obviously not tried searching! – Nick Bull Jun 26 '12 at 11:18
  • @RKK If i not tried how do now about regular expression! i confused how to use $9-0 like this symbol in my coding! – user1478583 Jun 26 '12 at 11:19
  • do you really need regular expressions? if you want to allow only digital enter, you can make something like in answer to this question http://stackoverflow.com/questions/1846777/defaulting-iphone-numeric-keypad – Morion Jun 26 '12 at 11:22
  • @user1478583 You may know about regular expressions, but that doesn't mean you've actually tried to do anything. Have you searched for other similar questions (hint - there are a LOT of them), searched the documentation, searched anywhere? Tried writing some code and then got some problem with it (if so, post your code and explain the problem you are having). We are here to help you, but you have to do some work yourself before asking – Nick Bull Jun 26 '12 at 11:22
  • @ Nick Bull Thanks for your advice for i will check when i am posting the question! i am new to stackoverflow thanks a lot for your advice NickBull – user1478583 Jun 26 '12 at 11:25

4 Answers4

3

In your shouldChangeCharactersInRange you can do it by this

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

        NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];

        NSString *expression = @"^([0-9]+$";

        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;
}
Mihir Mehta
  • 13,743
  • 3
  • 64
  • 88
2

Try this for your regular expression ^[0-9]+$

kiran
  • 4,285
  • 7
  • 53
  • 98
1

Try this, it would be helpfull to you

- (void)textFieldDidEndEditing:(UITextField *)textField 
{
    activeField = nil;
    if (textField == tfMobile) 
    {
        NSString *strRegExp=@"[0-9]*";
        NSPredicate *MobNumTest=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",strRegExp];
        if([MobNumTest evaluateWithObject:textField.text]==NO)
        { 

            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Please Enter correct contact no." delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil];

            [alert show];
            [alert release];

            tfMobile.text =@"";

        }

    }
    }
Ravi
  • 1,759
  • 5
  • 20
  • 38
0

Call this method where you need.it returns true if checkString contains only numerals

- (BOOL) textFieldValidation:(NSString *)checkString {
       NSString *stricterFilterString = @"[0-9]+";//here + is for one or more numerals(use * for zero or more numerals)
       NSString *regEx =  stricterFilterString;
       NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regEx];
       return [emailTest evaluateWithObject:checkString];

}

Ab'initio
  • 5,368
  • 4
  • 28
  • 40