0

I need to remove/replace all characters except for alpha numeric, -, & from NSString. How do i construct my regular expression to achieve that I want to ensure user does not enter any characters other than alphanumeric, & and - in uitextfield. How can i do that. Previusly i Handled things in shoulchangecharactersineange, but now the filter is getting increasigly big to handle through if conditions. Regular expressions may be the best choice for my scenario. I'd apprecite your thoughts and inputs

1
  • Perhaps you can show us your shouldChangeCharactersInRange, but tc's answer shows how simple it can be. Whether you use his choice of 7-bit character set, or broaden it to support common European languages, like jonkroll did, is up to you. But either way, it should be pretty simple. Commented Jul 21, 2012 at 4:05

2 Answers 2

1
-(BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)r replacementString:(NSString*)s
{
  NSCharacterSet * reject = [[NSCharacterSet characterSetWithCharactersInString:@"QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890-&"] invertedSet];
  if ([s rangeOfCharacterFromSet:reject].length)
  {
    return NO;
  }
  return YES;
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can use the NSString method rangeOfCharctersFromSet: to do this check.

First construct a character set containing your allowable characters, and then used invertedSet to get a set of all non-allowed characters. Then search your string for the range of characters in this set. If none of the characters in the set are found the return range value will be {NSNotFound, 0}

NSMutableCharacterSet *legalCharacters = [NSCharacterSet alphanumericCharacterSet];
[legalCharacters addCharactersInString:@"-&"];
NSCharacterSet *illegalCharacters = [legalCharacters invertedSet];

NSString *testString = @"ThisStringWillFail@#$%";

if ([testString rangeOfCharacterFromSet:illegalCharacters].location == NSNotFound) {
    // string passed validation
} else {
    // string failed validation
}

2 Comments

[NSCharacterSet alphanumericCharacterSet] returns the set of Unicode "letters" and "digits" (including all Chinese characters, for example). This is probably not what is intended.
It's up to the original question's author as to how restrictive to be, but I personally hate apps that reject common accented characters for no good reason. If this is, for example, capturing proper names, it shouldn't force someone to change their name because the app lives in a 7-bit world! I personally would err towards jonkroll's character set (but use it in shouldChangeCharactersInRange) unless there were some app requirements that precluded it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.