Verify that NSString contains only alphanumeric + underscores

I have a string that should only be az, 0-9 and _

How to check the correct input? I tried this, but it accepts a letter like Γ₯, Γ€, ΓΆ, ΓΈ etc.

NSString *string = [NSString stringWithString:nameField.text]; NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet]; [string stringByTrimmingCharactersInSet:alphaSet]; [string stringByReplacingOccurrencesOfString:@"_" withString:@""]; BOOL valid = [[string stringByTrimmingCharactersInSet:alphaSet] isEqualToString:@""]; 
+50
ios objective-c
Sep 25 '11 at 14:44
source share
7 answers

You can create your own character set:

 NSCharacterSet *s = [NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"]; 

After that, you flip it to anything that is not in the original line:

 s = [s invertedSet]; 

And then you can use the string method to find out if your string contains anything in the inverted set:

 NSRange r = [string rangeOfCharacterFromSet:s]; if (r.location != NSNotFound) { NSLog(@"the string contains illegal characters"); } 
+126
Sep 25 2018-11-15T00:
source share

You can use the predicate:

 NSString *myRegex = @"[A-Z0-9a-z_]*"; NSPredicate *myTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", myRegex]; NSString *string = nameField.text; BOOL valid = [myTest evaluateWithObject:string]; 

Edit: I did not notice that you are using [NSString stringWithString:nameField.text] .

Use nameField.text instead.

+35
Sep 25 2018-11-11T00:
source share

A slightly simpler way

 NSMutableCharacterSet *allowedSet = [NSMutableCharacterSet characterSetWithCharactersInString:@"_"]; [allowedSet formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]]; NSCharacterSet *forbiddenSet = [allowedSet invertedSet]; 

It will combine alphanumeric characters with _underscore.

you can use it for example

 NSRange r = [string rangeOfCharacterFromSet:forbiddenSet]; if (r.location != NSNotFound) { NSLog(@"the string contains illegal characters"); } 

PS. example copied from @DaveDeLong example :)

+8
Oct. 16 '14 at 12:33
source share

Create your own character set using [NSCharacterSet characterSetWithCharactersInString:] , then trim as you did to see if the returned string has a length value.

Or you can use invertedSet to remove all unspecified characters if this helps create a cleared string.

+4
Sep 25 2018-11-11T00:
source share

You can scroll through each character in a line and check that it is alphanumeric:

 BOOL isMatch = YES; for (int i = 0; i < [string length]; i++) { unichar c = [string characterAtIndex:i]; if (!isalnum(c) && c != '_') { isMatch = NO; break; } } if (isMatch) { // valid } else { // invalid } 
+3
Sep 25 '11 at 14:50
source share

Here you can do it in Swift (as an extension of the String class):

 extension String { func containsValidCharacters() -> Bool { var charSet = NSCharacterSet(charactersInString: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_") charSet = charSet.invertedSet let range = (self as NSString).rangeOfCharacterFromSet(charSet) if range.location != NSNotFound { return false } return true } } 
+1
Dec 08 '15 at 6:36
source share

This is a quick helper if he helps.

 -(BOOL)isString:(NSString *)s{ char letter = 'A'; //BOOL isLetter=NO; for (int i=0; i<26; i++) { NSString *temp = [NSString stringWithFormat:@"%c",letter+i]; if ([s isEqualToString:temp]) { return YES; } } return NO; } 
-one
Jun 07 '13 at 19:34
source share



All Articles