Note that there are some explanatory texts on larger screens.

plurals
  1. POUITextField to allow ONLY English letters inputs
    primarykey
    data
    text
    <p>I would like restrict input to ONLY english alphabets (a-z) for the username field. Can someone please help me to add to the code below to NOT allow input of any numbers, characters, and any other languages, other than the English alphabets? </p> <p>Right now the code below does not allow white spaces and nor does it allow input of more than 10 letters.</p> <pre><code>- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { { BOOL _isAllowed = YES; NSString *nameRegex = @"[A-Za-z]+"; NSPredicate *nameTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", nameRegex]; NSString *tempString = [[textField.text stringByReplacingCharactersInRange:range withString:string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; if (textField == self.usernameField) { if ([self.usernameField.text isEqualToString:tempString]) { self.title.text = @"No Spaces Allowed"; self.title.textColor = [UIColor yellowColor]; _isAllowed = NO; } else if (tempString.length &gt; 10) { self.title.text = @"Username cannot be more than 10 letters."; self.title.textColor = [UIColor yellowColor]; _isAllowed = NO; } </code></pre> <p><strong>UPDATED</strong>:</p> <pre><code> else if (![nameTest evaluateWithObject:self.usernameField.text]) { self.title.text = @"Username can only contain english letters."; self.title.textColor = [UIColor yellowColor]; _isAllowed = NO; } return _isAllowed; } return _isAllowed; } </code></pre> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload