ios正则表达式
#pragma mark -验证手机号码的正则表达式
创新互联专业为企业提供邕宁网站建设、邕宁做网站、邕宁网站设计、邕宁网站制作等企业网站建设、网页设计与制作、邕宁企业网站模板建站服务,十余年邕宁做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
- (BOOL)checkTel:(NSString *)str
{
if ([strlength] ==0) {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"手机号不能为空"message:nildelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alert show];
returnNO;
}
NSString *regex =@"^((13[0-9])|(147)|(15[^4,\\D])|(18[01,5-9]))\\d{8}$";
NSPredicate *pred = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", regex]; BOOL isMatch = [predevaluateWithObject:str];
if (!isMatch) {
UIAlertView *alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:@"请输入正确的手机号码"delegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nil,nil];
[alertshow];
returnNO;
}
returnYES;
}
#pragma mark -邮箱验证
-(BOOL)isValidateEmail:(NSString *)email
{
NSString *emailRegex =@"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicatepredicateWithFormat:@"SELF MATCHES %@", emailRegex];
return [emailTestevaluateWithObject:email];
}
#pragma mark -验证***是否合法
-(BOOL) chk18PaperId:(NSString *) sPaperId
{
//判断位数
if ([sPaperIdlength] !=15 && [sPaperIdlength] !=18) {
returnNO;
}
NSString *carid = sPaperId;
long lSumQT =0;
//加权因子
int R[] ={7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2 };
//校验码
unsignedchar sChecker[11]={'1','0','X','9','8','7','6','5','4','3','2'};
//将15位***号转换成18位
NSMutableString *mString = [NSMutableStringstringWithString:sPaperId];
if ([sPaperIdlength] ==15) {
[mStringinsertString:@"19"atIndex:6];
long p =0;
constchar *pid = [mStringUTF8String];
for (int i=0; i<=16; i++)
{
p += (pid[i]-48) * R[i];
}
int o = p%11;
NSString *string_content = [NSStringstringWithFormat:@"%c",sChecker[o]];
[mStringinsertString:string_contentatIndex:[mStringlength]];
carid = mString;
}
//判断地区码
NSString * sProvince = [caridsubstringToIndex:2];
if (![selfareaCode:sProvince]) {
returnNO;
}
//判断年月日是否有效
//年份
int strYear = [[selfgetStringWithRange:caridValue1:6Value2:4]intValue];
//月份
int strMonth = [[selfgetStringWithRange:caridValue1:10Value2:2]intValue];
//日
int strDay = [[selfgetStringWithRange:caridValue1:12Value2:2]intValue];
NSTimeZone *localZone = [NSTimeZonelocalTimeZone];
NSDateFormatter *dateFormatter = [[NSDateFormatteralloc]init];
[dateFormattersetDateStyle:NSDateFormatterMediumStyle];
[dateFormattersetTimeStyle:NSDateFormatterNoStyle];
[dateFormattersetTimeZone:localZone];
[dateFormattersetDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date=[dateFormatterdateFromString:[NSStringstringWithFormat:@"%d-%d-%d 12:01:01",strYear,strMonth,strDay]];
if (date ==nil) {
returnNO;
}
constchar *PaperId = [caridUTF8String];
//检验长度
if(18 !=strlen(PaperId))return -1;
//校验数字
for (int i=0; i<18; i++)
{
if ( !isdigit(PaperId[i]) && !(('X' == PaperId[i] ||'x' == PaperId[i]) &&17 == i) )
{
returnNO;
}
}
//验证最末的校验码
for (int i=0; i<=16; i++)
{
lSumQT += (PaperId[i]-48) * R[i];
}
if (sChecker[lSumQT%11] != PaperId[17] )
{
returnNO;
}
returnYES;
}
/**
*功能:判断是否在地区码内
*参数:地区码
*/
-(BOOL)areaCode:(NSString *)code
{
NSMutableDictionary *dic = [[NSMutableDictionaryalloc]init];
[dicsetObject:@"北京"forKey:@"11"];
[dicsetObject:@"天津"forKey:@"12"];
[dicsetObject:@"河北"forKey:@"13"];
[dicsetObject:@"山西"forKey:@"14"];
[dicsetObject:@"内蒙古"forKey:@"15"];
[dicsetObject:@"辽宁"forKey:@"21"];
[dicsetObject:@"吉林"forKey:@"22"];
[dicsetObject:@"黑龙江"forKey:@"23"];
[dicsetObject:@"上海"forKey:@"31"];
[dicsetObject:@"江苏"forKey:@"32"];
[dicsetObject:@"浙江"forKey:@"33"];
[dicsetObject:@"安徽"forKey:@"34"];
[dicsetObject:@"福建"forKey:@"35"];
[dicsetObject:@"江西"forKey:@"36"];
[dicsetObject:@"山东"forKey:@"37"];
[dicsetObject:@"河南"forKey:@"41"];
[dicsetObject:@"湖北"forKey:@"42"];
[dicsetObject:@"湖南"forKey:@"43"];
[dicsetObject:@"广东"forKey:@"44"];
[dicsetObject:@"广西"forKey:@"45"];
[dicsetObject:@"海南"forKey:@"46"];
[dicsetObject:@"重庆"forKey:@"50"];
[dicsetObject:@"四川"forKey:@"51"];
[dicsetObject:@"贵州"forKey:@"52"];
[dicsetObject:@"云南"forKey:@"53"];
[dicsetObject:@"西藏"forKey:@"54"];
[dicsetObject:@"陕西"forKey:@"61"];
[dicsetObject:@"甘肃"forKey:@"62"];
[dicsetObject:@"青海"forKey:@"63"];
[dicsetObject:@"宁夏"forKey:@"64"];
[dicsetObject:@"新疆"forKey:@"65"];
[dicsetObject:@"台湾"forKey:@"71"];
[dicsetObject:@"香港"forKey:@"81"];
[dicsetObject:@"澳门"forKey:@"82"];
[dicsetObject:@"国外"forKey:@"91"];
if ([dicobjectForKey:code] ==nil) {
returnNO;
}
returnYES;
}
/**
*功能:获取指定范围的字符串
*参数:字符串的开始小标
*参数:字符串的结束下标
*/
-(NSString *)getStringWithRange:(NSString *)str Value1:(NSInteger)value1 Value2:(NSInteger )value2;
{
return [strsubstringWithRange:NSMakeRange(value1,value2)];
}
网页标题:ios正则表达式
分享链接:http://pwwzsj.com/article/pdcehd.html