ios - 检查 UITextfield 是否包含 (? ! $ 等)

标签 ios if-statement nscharacterset

我正在执行一个条件语句来检查文本字段是否包含某些字符,如果包含,它们将显示 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/

相关文章:

c# - 如果不为空,检查查询字符串参数值的最优雅方法?

python - 简单IF的语法错误。

Excel VBA : "For" and "If" statement on a single line?

ios - 将数据传回 View Controller 的问题

ios - iPhone 如何学习新的 WiFi 位置并将其用于位置估计

ios - 为什么我在使用它之前不必先实例化 NSCharacterSet 对象?

swift - 如何检查字符串是否包含 Swift 中的字母?

c++ - 如何在 rc 文件中设置对话框代码页

ios - 相互交换 Sprite 位置

ios - iPhone : offset with compass heading values between landscape right and landscape left