0

I'm making the pattern to check the user can only enter number and string. Please anyone can help me or give some reference. Following is my code. Thnks in advance.

NSString *regEx =
     @"(?:[a-z0-9!#$%\\&'*+/=?\\^_`{|}~-]+(?:\\.[a-z0-9!#$%\\&'*+/=?\\^_`{|}"
     @"~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\"
     @"x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-"
     @"z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5"
     @"]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-"
     @"9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21"
     @"-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])";

NSString *strRoadworthyGet=@"vinod123";
NSPredicate *matchPattern =[NSPredicate predicateWithFormat:@"SELF MATCHES %@",regEx];
BOOL resultMatch =[matchPattern evaluateWithObject:strRoadworthyGet];

if(!resultMatch)
  NSLog(@"invalid pattern");
4

2 に答える 2

4

You can use NSCharacterSet's alphanumeric method to return check for Letters, Marks and Numbers.

If you want to go for RegularExpression, you can use

NSString *regEx = @"^[a-zA-Z0-9]*$";
于 2012-06-11T12:02:08.553 に答える
1

Defining legal input characters

于 2012-06-11T12:02:24.737 に答える