Проверьте, содержит ли поле UIText (?! $ И т. Д.)

Я делаю условный оператор, который проверяет, содержит ли текстовое поле определенные символы, если он это делает, UIAlertView покажет. В настоящее время, если текстовое поле содержит буквы, отображается предупреждение, как бы я расширил свой метод ниже, чтобы включить такие символы, как?!, $ и т. д.

if ([self.withdrawTextField.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]].location != NSNotFound) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Ooops" message:@"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

}

2 ответа

Решение

Попробуй это:

NSMutableCharacterSet *mcs1 = [[NSCharacterSet letterCharacterSet] mutableCopy];
 [mcs1 addCharactersInString:@"?!.$"];

if ([self.withdrawTextField.text rangeOfCharacterFromSet:mcs1].location != NSNotFound) {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Ooops" message:@"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];

}

Поскольку вы хотите только цифры и точки в строке, чем вы можете попробовать ниже код. Это может помочь вам.

NSCharacterSet *myCharSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
for (int i = 0; i < [[self.withdrawTextField.text length]; i++) 
{
    unichar c = [string characterAtIndex:i];

    if (![myCharSet characterIsMember:c]) 
       {
        UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Ooops" message:@"Please only use numbers and the period (.)." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
        return;
       }
}
Другие вопросы по тегам