仅允许UITextField的字母和数字

use*_*977 6 iphone if-statement objective-c uitextfield ios

我有一个注册视图控制器,用户需要输入电子邮件地址,用户名和密码.

对于用户名和密码字段,您现在可以使用"user%$&"这样的用户名和"password%^&$"之类的密码进行注册.我不希望人们能够使用这些特殊字符.

我想这样做,以便用户名和密码字段只能用字母数字字符(字母和数字)提交.我找到了一些方法来做到这一点,但它们令人困惑,我需要它专门用于我的代码.

以下是用户输入电子邮件,用户名和密码并按下提交按钮时执行的一些代码:

NSString *user = [_usernameEntry text];
NSString *pass = [_passwordEntry text];
NSString *email = [_emailEntry text];

self.userSubmittedUsername = user;

if ([user length] < 4 || [pass length] < 4) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Entry" message:@"Username and Password must both be at least 4 characters long." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];

    [alert show];

} else if ([email length] < 8) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Entry" message:@"Please enter your email address." delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil];

    [alert show];

} else {
Run Code Online (Sandbox Code Playgroud)

我只想else if在上面的代码中添加另一个代码,如果有任何字符或数字字符,它会显示一个警告视图.

谢谢.

Gen*_*ted 16

如果你真的想限制用户输入特殊字符,那么最好的方法就是使用shouldChangeCharactersInRange:方法

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField==YOUR_USER_NAME_TEXTFIELD || textField == YOUR_PASSWORD_TEXTFIELD)
    {

        NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"];
        for (int i = 0; i < [string length]; i++)
        {
            unichar c = [string characterAtIndex:i];
            if (![myCharSet characterIsMember:c])
            {
                return NO;
            }
        }

        return YES;
    }

    return YES;
}
Run Code Online (Sandbox Code Playgroud)

这将限制用户输入特殊字符.正如rmaddy指出的那样,在数据输入发生时进行这种验证总是好的.

PS:确保为文本字段设置委托.


sch*_*ler 12

如果有人最终在这里寻找快速方式在Swift中执行此操作,这是我的解决方案(其中UITextField允许使用大写字母,小写字母,数字和空格.)

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    let set = NSCharacterSet(charactersInString: "ABCDEFGHIJKLMONPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ").invertedSet
    return string.rangeOfCharacterFromSet(set) == nil

}
Run Code Online (Sandbox Code Playgroud)

如果它不是用于空间,你可以简单地使用

let set = NSCharacterSet.alphanumericCharacterSet().invertedSet
Run Code Online (Sandbox Code Playgroud)

在每次按键时,我们检查输入的字符是否包含在我们的样本集中,否则,我们返回false.


Hem*_*ang 7

一些更好的和更好的方式,

好办法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
    if(textField.tag == yourTextFieldTag) {
        NSCharacterSet *allowedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
        return ([characters rangeOfCharacterFromSet:allowedCharacters].location == NSNotFound);
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)

更好的方法

请注意,甚至你可以把它的内存使用效率通过声明NSCharacterSet *allowedCharacters.h文件(头文件),并且在viewDidLoad你可以这样写,

allowedCharacters = [[NSCharacterSet alphanumericCharacterSet] invertedSet];
Run Code Online (Sandbox Code Playgroud)

然后你可以像这样使用它,

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)characters
{
    if(textField.tag == yourTextFieldTag) {
        return ([characters rangeOfCharacterFromSet:allowedCharacters].location == NSNotFound);
    }
    return YES;
}
Run Code Online (Sandbox Code Playgroud)