1

I want to check the string if it contains alphanumeric only.(Both letters and number only). I tried using NSCharacterSet *strCharSet = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"];

I also tried this NSString *string = @"[^a-zA-Z0-9]"; = no luck

but it only checks if the string does not contain any characters or it contains only this characters. I need to required the user to put both letters and numbers.

-update I have a textfield for password. I need the user to input both letters and password. But I can't check if the textfield contains BOTH letters and numbers. By using the code above, it only checks if the textfield's text contains only alphanumeric.

Wain
  • 118,658
  • 15
  • 128
  • 151
MaappeaL
  • 514
  • 1
  • 8
  • 16
  • try this link http://stackoverflow.com/questions/7541803/allow-only-alphanumeric-characters-for-a-uitextfield – Anbu.Karthik Sep 20 '14 at 10:53
  • it works the same, it just allow user to input alphanumeric, what i want is to required the user to input numbers and letter. – MaappeaL Sep 20 '14 at 11:10
  • You haven't really shown enough code to know HOW you are checking or what you are trying to accomplish . Also note, your approach will be limited to English and fail with most other languages. – uchuugaka Sep 20 '14 at 11:14
  • Show all the code you're using. If you want 2 different checks, don't combine the data that you use to make those checks... – Wain Sep 20 '14 at 11:19
  • 1
    Try this [String rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet]].location != NSNotFound && [String rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]].location != NSNotFound – NANNAV Sep 20 '14 at 11:23

1 Answers1

8

You just need to make your checks for numbers and letters separate.

BOOL containsLetter = NSNotFound != [input rangeOfCharacterFromSet:NSCharacterSet.letterCharacterSet].location;
BOOL containsNumber = NSNotFound != [input rangeOfCharacterFromSet:NSCharacterSet.decimalDigitCharacterSet].location;

NSLog(@"Contains letter: %d\n Contains number: %d", containsLetter, containsNumber);
Paul.s
  • 38,494
  • 5
  • 70
  • 88