ios电话/密码/验证码/身份证的正则表达式
RegexKitLite 这个第三方库可用验证正则
// 一 .电话号码正则表达式
-(BOOL)testPhoneNumber:(NSString *)text
{
// #define phoneMatchingText @"^1[3-9][0-9]\\d{4,8}$"
// @"[1][3-9]\\d{9}"
NSString *regex =@"(13[0-9]|0[1-9]|0[1-9][0-9]|0[1-9][0-9][0-9]|15[0-9]|18[02356789])\\d{8}|17\\d{9}";
NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [mobileTest evaluateWithObject:text];
}
// 二.
/**< 手机校验正则 */
- (BOOL)phoneNumberIsTure:(UITextField *)textField
{
NSString *phoneNumeberIsTureStr = @"^1[0-9][0-9]\\d{4,8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneNumeberIsTureStr];
BOOL isTure = [pred evaluateWithObject:textField.text];
return isTure;
}
//验证码正则
-(BOOL)testCodeNumber:(NSString *)text
{
NSString *regex =@"\\d{6}";
NSPredicate *mobileTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [mobileTest evaluateWithObject:text];
}
//密码校验 - 修改密码正则
- (BOOL)passWordIsTure:(UITextField *)textField
{
NSString *passWordGex = @"^(?![A-Z]*$)(?![a-z]*$)(?![0-9]*$)(?![^a-zA-Z0-9]*$)\\S+$";
NSPredicate *passWordPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordGex];
BOOL isPassWord = [passWordPred evaluateWithObject:textField.text];
return isPassWord;
}
// 获取验证码按钮 -- 通过改方法获取验证码倒计时
- (IBAction)smsBtnClick:(UIButton *)sender {
NSLog(@"获取验证码按钮");
if (![self testPhoneNumber:self.phoneNum.text]) {
HB_ALERT_AFTER(@"请正确输入手机号码", 1.5);
return;
}
// 交出第一响应者
[self.phoneNum resignFirstResponder];
[self.putSmsNum resignFirstResponder];
// self.smsCodeBtn.userInteractionEnabled =NO;
// UIView *view = [[UIView alloc]initWithFrame:sender.bounds];
// view.alpha = 0.5;
// [sender addSubview:view];
[sender setSelected:YES];
// 获取验证码
NSMutableDictionary *att = [NSMutableDictionary dictionary];
[att setObject:self.phoneNum.text forKey:@"mobile"];
[att setObject:@"1" forKey:@"status"];
[HBRequest postURLString:@"/api/user/xxxxx" parameters:att success:^(id responseObject) {
NSLog(@"res -- %@",responseObject);
HB_ALERT_AFTER(@"已将验证码发送到您的手机", 1.0f);
if (sender.userInteractionEnabled == YES) {
sender.titleLabel.font = [UIFont systemFontOfSize:12];
__block int timeout = 59;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
if (timeout <= 0) {
dispatch_source_cancel(timer);
dispatch_async(dispatch_get_main_queue(), ^{
sender.userInteractionEnabled = YES;
self.phoneNum.userInteractionEnabled =YES;
sender.titleLabel.font = [UIFont systemFontOfSize:12];
[sender setTitle:[NSString stringWithFormat:@"重发"] forState:UIControlStateNormal];
});
}else{
int seconds = timeout % 60;
NSString *strTime = [NSString stringWithFormat:@"%.2d",seconds];
dispatch_async(dispatch_get_main_queue(), ^{
[sender setTitle:[NSString stringWithFormat:@"%@秒后重发",strTime] forState:UIControlStateNormal];
sender.userInteractionEnabled = NO;
self.phoneNum.userInteractionEnabled = NO;
});
timeout--;
}
});
dispatch_resume(timer);
}else{
}
} failure:^(NSError *error) {
NSLog(@"err -- %@",error);
}];
}
//---------------身份证正则---------------
- (BOOL)checkId5:(NSString *)idStr
{
NSString * prex = @"^(^\\d{15}$|^\\d{18}$|^\\d{17}(\\d|X|x))$";
if ([idStr isMatchedByRegex:prex]) {
return YES;
}
return NO;
}