仅允许UITextField的字母和数字 [英] Only allow letters and numbers for a UITextField

查看:169
本文介绍了仅允许UITextField的字母和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a signup view controller where the user needs to enter an email address, a username, and a password.

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

For the username and password fields, right now you can currently sign up with a username like "user%$&" and a password like "password%^&$". I don't want people to be able to use these special characters.

我想这样做,以便用户名和密码字段只能用字母数字字符提交字母和数字。我找到了一些方法来做到这一点,但是它们让我感到困惑,我需要这个方法专门用我的代码。

I want to make it so that the username and password fields can only be submitted with alphanumeric characters aka letters and numbers. I have found some methods to do this, but they are confusing and I need this to work specifically with my code.

这是我执行的一些代码用户输入了电子邮件,用户名和密码,然后按下提交按钮:

Here is some of the code that I perform when a user has entered an email, username, and password and they press the submit button:

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 {

我只想添加另一个否则,如果进入上述代码,并且如果有任何非字母或数字的字符,则显示警告视图。

I would just like to add another else if into the above code and have it show an alert view if there are any characters that are NOT letters or numbers.

谢谢。

推荐答案

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

If you really want to restrict user from entering special characters, then probably the best way is to use shouldChangeCharactersInRange: method

-(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;
}

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

This will restrict the user from entering the special characters. As rmaddy pointed out, it is always good to do this kind of validation when data entry happens.

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

PS: Make sure you set the delegate for your textfields.

这篇关于仅允许UITextField的字母和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆