2

How to use regular expression in iOS sdk?

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

6
  • 1
    What have you tried Jun 26, 2012 at 11:17
  • @RKK - Obviously not tried searching!
    – Nick Bull
    Jun 26, 2012 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! Jun 26, 2012 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 stackoverflow.com/questions/1846777/…
    – Morion
    Jun 26, 2012 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, 2012 at 11:22

4 Answers 4

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

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

0
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 =@"";

        }

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.