0

アプリにログインするときに人々が使用するユーザー名を検証する必要があります。例えば:

Peter$@ は無効ですが、peter123 は有効です

ユーザー名に .!#$%&'*+-/=?^_`{|}~@,; が含まれている場合 彼の名前で、ユーザーに通知するために alertView が表示されます

このような文字列を比較する必要がありますか?

-(BOOL) checkIfUsernameValidation{
    NSString *_username = playerName.text;
    NSString *expression = @".!#$%&'*+-/=?^_`{|}~@,;";

    if(![_username compare:expression]){
        return YES;
    }
    else
        return NO;
}

ありがとう

4

4 に答える 4

4

これを行う 1 つの方法は、 NSCharacterSetを使用することです。

たとえば、許可するすべての文字の文字セットを作成し、テキスト フィールドを見て、次のようなものを使用します。

NSCharacterSet * characterSetFromTextField = [NSCharacterSet characterSetWithCharactersInString: yourTextField];
if([[NSCharacterSet alphanumericCharacterSet] isSupersetOfSet: characterSetFromTextField] == NO)
{
    NSLog( @"there are bogus characters here, throw up a UIAlert at this point");
    return;
}

私は alphanumericCharacterSet を使用しましたが、 " characterSetWithCharactersInString"を使用して、許可されているすべての文字の独自の文字セットを簡単に作成できます。

于 2013-08-01T19:16:05.580 に答える
0
-(BOOL) checkIfUsernameValidation{
    NSString *_username = playerName.text;

    NSCharacterSet * set = [[NSCharacterSet characterSetWithCharactersInString:@".!#$%&'*+-/=?^_`{|}~@,;"] invertedSet];

    if ([_username rangeOfCharacterFromSet:set].location != NSNotFound){
        return YES;
    }
    else
        return NO;
    }
}
于 2013-08-01T19:28:02.153 に答える