ユーザー名フィールドの入力を英語のアルファベット (az) のみに制限したいと思います。英語のアルファベット以外の数字、文字、およびその他の言語の入力を許可しないように、以下のコードに追加するのを手伝ってもらえますか?
現在、以下のコードは空白を許可しておらず、10 文字を超える入力も許可していません。
- (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 > 10)
{
self.title.text = @"Username cannot be more than 10 letters.";
self.title.textColor = [UIColor yellowColor];
_isAllowed = NO;
}
更新:
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;
}
ありがとう。