我正在执行一个条件语句来检查文本字段是否包含某些字符,如果包含,它们将显示 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];
}
最佳答案
试试这个:
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];
}
关于ios - 检查 UITextfield 是否包含 (? ! $ 等),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20143036/