Vinod Vishwakarma
Vinod Vishwakarma

Reputation: 87

Regular expression to allow only string and number

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");

Upvotes: 0

Views: 6643

Answers (2)

krammer
krammer

Reputation: 2658

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]*$";

Upvotes: 4

Mani
Mani

Reputation: 1841

Defining legal input characters

Upvotes: 0

Related Questions