Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Only allow letters and numbers for a UITextField

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

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.

Thanks.

like image 401
user3344977 Avatar asked Mar 08 '14 04:03

user3344977


People also ask

How do I restrict Uitextfield to take only numbers in Swift?

Method 1: Changing the Text Field Type from storyboard. Select the text field that you want to restrict to numeric input. Go to its attribute inspector. Select the keyboard type and choose number pad from there.

How do you dismiss a keyboard in Swift?

Via Tap Gesture This is the quickest way to implement keyboard dismissal. Just set a Tap gesture on the main View and hook that gesture with a function which calls view. endEditing . Causes the view (or one of its embedded text fields) to resign the first responder status.


1 Answers

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

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: Make sure you set the delegate for your textfields.

like image 145
GenieWanted Avatar answered Nov 10 '22 08:11

GenieWanted