【发布时间】:2023-03-07 14:58:01
【问题描述】:
嗨,我是 Ios 的初学者,在我的项目登录页面中有文本字段验证(这里我有两个文本字段,它们是 medicaId、密码)
我的医疗补助必须只允许数字和 medicaId 以 10 开头并以 99 结尾,如果不是,那么我想显示警报 Invalid medicaId
但根据我的经验,我只能做文本字段允许数字,但我不明白如何做那个文本字段(我的医疗补助)以 10 开头并以 99 结尾,请帮帮我
我的代码:-
#import "ViewController.h"
@interface ViewController ()
#define ACCEPTABLE_CHARECTERS @"0123456789"
@end
@implementation ViewController
@synthesize textfield;
- (void)viewDidLoad {
[super viewDidLoad];
textfield.delegate = self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField==textfield)
{
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:ACCEPTABLE_CHARECTERS] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
return [string isEqualToString:filtered];
}
return YES;
}
-(IBAction)CickMe:(id)sender{
if (success) {
NSLog(@"validations are perfect");
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"MedicaId must starts with 10 and ends with 99" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
[alert show];
}
}
【问题讨论】:
-
使用
regular expression。 -
请用代码@luk2302解释一下
-
请显示一些确切允许输入的示例和一些应该拒绝的输入,我还没有完全理解您的要求
-
@luk2302 很可能,因为正则表达式没有任何帮助
-
我的要求是我希望文本字段只允许“数字”并且它必须以 10 开头并以 99 结束
标签: ios objective-c uitextfield