一. 手机号
- 限定手机号长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *tmpStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (_telField==textField) {
if ([tmpStr length]>11) {
_telField.text = [tmpStr substringToIndex:11];
return NO;
}
}
return YES; }
2). 手机号规则限定
- (BOOL)isTelNumber:(NSString *)telNum {
NSString *telStr = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";
NSPredicate *regextestTel = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",telStr];
return [regextestTel evaluateWithObject:telNum];
二. 身份证号
/ 身份证号验证
+(BOOL)validateIdentityCard:(NSString*)cardNo{
if (cardNo.length != 18) { return NO; }
NSArray* codeArray = [NSArray arrayWithObjects:@"7",@"9",@"10",@"5",@"8",@"4",@"2",@"1",@"6",@"3",@"7",@"9",@"10",@"5",@"8",@"4",@"2",nil];
NSDictionary* checkCodeDic = [NSDictionary dictionaryWithObjects:[NSArrayarrayWithObjects: @"1",@"0",@"X",@"9",@"8",@"7",@"6",@"5",@"4",@"3",@"2", nil] forKeys:[NSArray arrayWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil]];
NSScanner* scan = [NSScanner scannerWithString:[cardNo substringToIndex:17]];
int val;
BOOL isNum = [scan scanInt:&val] && [scan isAtEnd]
;
if (!isNum) { return NO; }
int sumValue = 0;
for (int i =0; i<17; i++) {
sumValue+=[[cardNo substringWithRange:NSMakeRange(i , 1) ]
intValue]* [[codeArray objectAtIndex:i] intValue];
}
NSString* strlast = [checkCodeDic objectForKey:[NSStringstringWithFormat:@"%d",sumValue%11]];
if ([strlast isEqualToString: [[cardNo substringWithRange:NSMakeRange(17,1)]uppercaseString]]) {
return YES; }
return NO;
}
三 判定邮箱是否正确
-(BOOL)validateEmail:(NSString *)email
{
NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
if( [emailTest evaluateWithObject:email]){
NSLog(@"恭喜!您输入的邮箱验证合法");
return YES;
}else{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的邮箱" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return NO;
}
return NO;
}