Abizer Nasir avatar Abizer Nasir committed bddf4da

Add a length validation as well

Comments (0)

Files changed (2)


 @implementation Validator
 + (BOOL)isValidString:(NSString *)string {
+    // I see you also have a length validation to do as well, filter for that first
+    NSUInteger inputLength = [string length];
+    if (!(inputLength > 0 && inputLength <= 30)) {
+        return NO; // bail and return NO
+    }
+    // Create a static comparison set for performance
+    // Basically - specify the character set you want and then invert the set to get a set of characters you don't want.
     static NSCharacterSet *unwantedCharacters = nil;
     if (!unwantedCharacters) {
         unwantedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
     NSRange rangeOfUnwantedCharacters = [string rangeOfCharacterFromSet:unwantedCharacters];
+    // If any characters are found from the invalid character set, they will have a valid range.
     if (rangeOfUnwantedCharacters.location == NSNotFound) {
         return YES;
     } else {
     @autoreleasepool {
         NSString *goodString = @"abcdefghijklmnopqrstuvwxyz0123456789";
         NSString *badString = @"[this & this are wrong]";
+        NSString *tooLongString = @"abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz0123456789";
         NSLog(@"Good String is valid: %@", [Validator isValidString:goodString] ? @"YES" : @"NO");
         NSLog(@"Bad String is valid: %@", [Validator isValidString:badString] ? @"YES" : @"NO");
+        NSLog(@"Too Long String is valid: %@", [Validator isValidString:tooLongString] ? @"YES" : @"NO");
     return 0;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.