//
// 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

ios表单验证帮助类的更多相关文章

  1. java后台表单验证工具类

    /** * 描述 java后台表单验证工具类 * * @ClassName ValidationUtil * @Author wzf * @DATE 2018/10/27 15:21 * @VerSi ...

  2. java工具类(二)之java正则表达式表单验证

    java正则表达式表单验证类工具类(验证邮箱.手机号码.qq号码等) 这篇文章主要介绍了java使用正则表达式进行表单验证工具类,可以验证邮箱.手机号码.qq号码等方法,需要的朋友可以参考下. jav ...

  3. Laravel教程 七:表单验证 Validation

    Laravel教程 七:表单验证 Validation 此文章为原创文章,未经同意,禁止转载. Laravel Form 终于要更新这个Laravel系列教程的第七篇了,期间去写了一点其他的东西. 就 ...

  4. 史上最全Java表单验证封装类

    package com.tongrong.utils; import java.util.Collection; import java.util.Map; import java.util.rege ...

  5. lumen手记:自定义Validate表单验证

    版权声明:本文为博主原创文章,未经博主允许不得转载. 今天开始跳lumen的表单验证Validate类的坑,确实好坑!!! 首先,lumen的表单验证返回是无状态的json格式api,这... 所有开 ...

  6. JS表单验证类HTML代码实例

    以前用的比较多的一个JS表单验证类,对于个人来说已经够用了,有兴趣的可以在此基础上扩展成ajax版本.本表单验证类囊括了密码验证.英文4~10个 字符验证. 中文非空验证.大于10小于100的数字.浮 ...

  7. 一个PHP常用表单验证类(基于正则)

    一个基于正则表达式的PHP常用表单验证类,作者:欣然随风.这个表单判断类的功能有:验证是否为指定长度的字母/数字组合.验证是否为指定长度汉字.身 份证号码验证.是否是指定长度的数字.验证邮件地址.电话 ...

  8. jdbc工具类的封装,以及表单验证数据提交后台

    在之前已经写过了jdbc的工具类,不过最近学习了新的方法,所以在这里重新写一遍,为后面的javaEE做铺垫: 首先我们要了解javaEE项目中,文件构成,新建一个javaEE项目,在项目中,有一个we ...

  9. php常用表单验证类用法实例

    <?php /** * 页面作用:常用表单验证类 * 作 者:欣然随风 * QQ:276624915 */ class class_post { //验证是否为指定长度的字母/数字组合 func ...

随机推荐

  1. mysql find_in_set()函数的使用

    mysql 中 find_in_set() 函数语法: FIND_IN_SET(str,strList) str 要查询的字符串 strList 字段名,参数以“,”分隔,如(1,2,6,8) 查询字 ...

  2. Two Melodies CodeForces - 813D (DP,技巧)

    https://codeforces.com/problemset/problem/813/D dp[i][j] = 一条链以i结尾, 另一条链以j结尾的最大值 关键要保证转移时两条链不能相交 #in ...

  3. 数据库连接的WEB登录界面的实现

    要实现此功能,需要电脑安装JAVA EE.SQL Server 2008和Tomcat等软件,并进行配置环境成功. 对这门课的希望和自己的目标: 希望:可以完全掌握老师所讲的内容. 目标:能够完整的做 ...

  4. 如何在VMware系统中的ubuntu16.04中建立与win7系统的共享文件夹

    点击虚拟机设置一次得到如图所示 系统默认放在了 /mnt/hgfs文件夹 点击虚拟机安装vmware tools 解压vmware tools压缩包 运行sudo ./vmware-install.p ...

  5. python-跨域问题

    跨域:因为浏览器的同源策略,在你请求返回的时候会进行拦截 jsonp 只能发 get 请求 cors 可以发任何请求 ,在响应时加个响应头就行 同源策略对ajax阻拦 同源策略对src或href属性的 ...

  6. 自用chrome+油猴脚本,使用迅雷下载百度云大文件,一键离线下载

    油猴是有名的火狐浏览器插件(Greasemonkey),当然也有Chrome版本(tampermonkey),甚至IE.Safari.Opera都有……虽然这些插件是由不同的开发者开发出来的,界面也可 ...

  7. 70. Climbing Stairs爬楼梯

    网址:https://leetcode.com/problems/climbing-stairs/ 其实就是斐波那契数列,没什么好说的. 注意使用3个变量,而不是数组,可以节约空间. class So ...

  8. 判断runtime是否运行在docker中及从docker中获取宿主机的ip信息

    1.判断运行时环境是否运行在docker中 参考:How to determine if a process runs inside lxc/Docker? 确定进程是否在LXC/Docker中运行? ...

  9. Spring Cloud系列之客户端请求带“Authorization”请求头,经过zuul转发后丢失了

    先摆解决方案: 方法一: 方法二: zuul.routes.<routeName>.sensitive-headers= zuul.routes.<routeName>.cus ...

  10. ubuntu14静态ip配置

    0.配置ip需要掌握的一些基本指令 打开/创建文件      sudo vim ... 插入信息      i 保存并强制退出      先按Esc,再键入:wq!,回车 1.使用命令 sudo vi ...