質問

I am allowing the user to copy a registration key from my website, the NSStirng is then saved to the clipboard and when the application opens with the registration dialog I check the clipboard right away using this code -

NSString *registrationCode = [UIPasteboard generalPasteboard].string;

Now that I have the value from the pasteboard I want to check its format.. which should be something like AAAA-AAAA-AAAA-AAAA so in essance No numbers, no spaces (including at the front and end), no lower case, no special characters... just uppercase characters. How could I achive this?

役に立ちましたか?

解決

Try doing this in your code:

NSCharacterSet * everythingThatsAllowedSet = [NSCharacterSet characterSetWithCharactersInString: @"ABCDEFGHIJKLMNOPQRSTUVWXYZ-"];
NSCharacterSet * characterSetFromRegistrationCode = [NSCharacterSet characterSetWithCharactersInString: registrationCode];

if([everythingThatsAllowedSet isSupersetOfSet: characterSetFromRegistrationCode ] == YES)
    NSLog( @"this is a valid registration code");
else
{
    // if there are any characters in the registration code that aren't members of
    // the everythingAllowed set, the "isSupersetOfSet" test will fail
    NSLog( @"this has invalid characters");
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top