ios表单验证帮助类

时间:2021-08-31 08:33:15
//
// ValidateHelper.h
//
#import <Foundation/Foundation.h> @interface ValidateHelper : NSObject //邮箱
+ (BOOL) validateEmail:(NSString *)email;
//手机号码验证
+ (BOOL) validateMobile1:(NSString *)mobile;
//车牌号验证
+ (BOOL) validateCarNo:(NSString *)carNo;
//车型
+ (BOOL) validateCarType:(NSString *)CarType;
//用户名
+ (BOOL) validateUserName:(NSString *)name;
//密码
+ (BOOL) validatePassword:(NSString *)passWord;
//昵称
+ (BOOL) validateNickname:(NSString *)nickname;
//身份证号
+ (BOOL) validateIdentityCardLength: (NSString *)identityCard;
/**
* 功能:验证身份证是否合法
* 参数:输入的身份证号
*/ + (BOOL) validateIdentityCard:(NSString *) sPaperId;
//手机号码验证
+ (BOOL)validateMobile:(NSString *)mobileNum;
@end //
// ValidateHelper.m
//
#import "ValidateHelper.h"
@implementation ValidateHelper
//邮箱
+ (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];
return [emailTest evaluateWithObject:email];
} //手机号码验证
+ (BOOL) validateMobile1:(NSString *)mobile
{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
} //车牌号验证
+ (BOOL) validateCarNo:(NSString *)carNo
{
NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
NSLog(@"carTest is %@",carTest);
return [carTest evaluateWithObject:carNo];
} //车型
+ (BOOL) validateCarType:(NSString *)CarType
{
NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
return [carTest evaluateWithObject:CarType];
} //用户名
+ (BOOL) validateUserName:(NSString *)name
{
NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
BOOL B = [userNamePredicate evaluateWithObject:name];
return B;
} //密码
+ (BOOL) validatePassword:(NSString *)passWord
{
NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
return [passWordPredicate evaluateWithObject:passWord];
} //昵称
+ (BOOL) validateNickname:(NSString *)nickName
{
NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [passWordPredicate evaluateWithObject:nickName];
} //身份证号
+ (BOOL) validateIdentityCardLength: (NSString *)identityCard
{
BOOL flag;
if (identityCard.length <= ) {
flag = NO;
return flag;
}
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
return [identityCardPredicate evaluateWithObject:identityCard];
} /**
* 功能:获取指定范围的字符串
* 参数:字符串的开始小标
* 参数:字符串的结束下标
*/ + (NSString *)getStringWithRange:(NSString *)str Value1:(NSInteger )value1 Value2:(NSInteger )value2; { return [str substringWithRange:NSMakeRange(value1,value2)]; } /**
* 功能:判断是否在地区码内
* 参数:地区码
*/ + (BOOL)validateAreaCode:(NSString *)code
{
NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; [dic setObject:@"北京" forKey:@""];
[dic setObject:@"天津" forKey:@""];
[dic setObject:@"河北" forKey:@""];
[dic setObject:@"山西" forKey:@""];
[dic setObject:@"内蒙古" forKey:@""];
[dic setObject:@"辽宁" forKey:@""];
[dic setObject:@"吉林" forKey:@""];
[dic setObject:@"黑龙江" forKey:@""];
[dic setObject:@"上海" forKey:@""];
[dic setObject:@"江苏" forKey:@""];
[dic setObject:@"浙江" forKey:@""];
[dic setObject:@"安徽" forKey:@""];
[dic setObject:@"福建" forKey:@""];
[dic setObject:@"江西" forKey:@""];
[dic setObject:@"山东" forKey:@""];
[dic setObject:@"河南" forKey:@""];
[dic setObject:@"湖北" forKey:@""];
[dic setObject:@"湖南" forKey:@""];
[dic setObject:@"广东" forKey:@""];
[dic setObject:@"广西" forKey:@""];
[dic setObject:@"海南" forKey:@""];
[dic setObject:@"重庆" forKey:@""];
[dic setObject:@"四川" forKey:@""];
[dic setObject:@"贵州" forKey:@""];
[dic setObject:@"云南" forKey:@""];
[dic setObject:@"*" forKey:@""];
[dic setObject:@"陕西" forKey:@""];
[dic setObject:@"甘肃" forKey:@""];
[dic setObject:@"青海" forKey:@""];
[dic setObject:@"宁夏" forKey:@""];
[dic setObject:@"*" forKey:@""];
[dic setObject:@"*" forKey:@""];
[dic setObject:@"香港" forKey:@""];
[dic setObject:@"澳门" forKey:@""];
[dic setObject:@"国外" forKey:@""]; if ([dic objectForKey:code] == nil) {
return NO;
}
return YES; } /**
* 功能:验证身份证是否合法
* 参数:输入的身份证号
*/ + (BOOL) validateIdentityCard:(NSString *) sPaperId
{
//判断位数
if ([sPaperId length] != && [sPaperId length] != ) {
return NO;
}
NSString *carid = sPaperId;
long lSumQT =;
//加权因子
int R[] ={, , , , , , , , , , , , , , , , }; //校验码
unsigned char sChecker[]={'','','X', '', '', '', '', '', '', '', ''};
//将15位身份证号转换成18位
NSMutableString *mString = [NSMutableString stringWithString:sPaperId]; if ([sPaperId length] == ) {
[mString insertString:@"" atIndex:];
long p = ;
const char *pid = [mString UTF8String];
for (int i=; i<=; i++)
{
p += (pid[i]-) * R[i];
}
int o = p%;
NSString *string_content = [NSString stringWithFormat:@"%c",sChecker[o]];
[mString insertString:string_content atIndex:[mString length]];
carid = mString;
}
//判断地区码
NSString * sProvince = [carid substringToIndex:];
if (![self validateAreaCode:sProvince]) {
return NO;
}
//判断年月日是否有效
//年份
int strYear = [[self getStringWithRange:carid Value1: Value2:] intValue];
//月份
int strMonth = [[self getStringWithRange:carid Value1: Value2:] intValue];
//日
int strDay = [[self getStringWithRange:carid Value1: Value2:] intValue]; NSTimeZone *localZone = [NSTimeZone localTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setTimeZone:localZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[dateFormatter dateFromString:[NSString stringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]]; if (date == nil) {
return NO;
} const char *PaperId = [carid UTF8String];
//检验长度
if( != strlen(PaperId)) return -;
//校验数字
for (int i=; i<; i++)
{
if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] || 'x' == PaperId[i]) && == i) )
{
return NO;
}
}
//验证最末的校验码
for (int i=; i<=; i++)
{
lSumQT += (PaperId[i]-) * R[i];
}
if (sChecker[lSumQT%] != PaperId[] )
{
return NO;
}
return YES;
} + (BOOL)validateMobile:(NSString *)mobileNum
{
/**
* 手机号码
* 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
* 联通:130,131,132,152,155,156,185,186
* 电信:133,1349,153,180,189 电信号段:133/153/180/181/189/177;
联通号段:130/131/132/155/156/185/186/145/176;
移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178。 *虚拟运营商:170
*/
NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|70|8[025-9])\\d{8}$";
/**
10 * 中国移动:China Mobile
11 * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
12 */
NSString * CM = @"^1(34[0-9]|(3[5-9]|5[0-9]|8[0-8])\\d)\\d{7}$";
/**
15 * 中国联通:China Unicom
16 * 130,131,132,152,155,156,185,186
17 */
NSString * CU = @"^1(3[0-2]|5[256]|8[56])\\d{8}$";
/**
20 * 中国电信:China Telecom
21 * 133,1349,153,180,189
22 */
NSString * CT = @"^1((33|53|8[019])[0-9]|349)\\d{7}$";
/**
25 * 大陆地区固话及小灵通
26 * 区号:010,020,021,022,023,024,025,027,028,029
27 * 号码:七位或八位
28 */
// NSString * PHS = @"^0(10|2[0-5789]|\\d{3})\\d{7,8}$"; NSString * XN = @"^170\\d{8}$";//虚拟运营商电话
NSString * XN1 = @"^177\\d{8}$";//虚拟运营商电话 NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT]; NSPredicate *regextestxn = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", XN];
NSPredicate *regextestxn1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", XN1]; if (([regextestmobile evaluateWithObject:mobileNum] == YES)
|| ([regextestcm evaluateWithObject:mobileNum] == YES)
|| ([regextestct evaluateWithObject:mobileNum] == YES)
|| ([regextestcu evaluateWithObject:mobileNum] == YES)
|| ([regextestxn1 evaluateWithObject:mobileNum] == YES)
|| ([regextestxn evaluateWithObject:mobileNum] == YES))
{
return YES;
}
else
{
return NO;
}
} @end