iOS各种验证、手机号验证、身份证号验证、银行卡号验证、邮箱验证
2017-07-20 本文已影响121人
疯得得
整理了之前用到的一些验证,发出来,希望对各位有用。
ValidateClass.h文件
//
// ValidateClass.h
// CustomProject
//
// Created by FengYiHao on 2017/7/20.
// Copyright © 2017年 FengYiHao. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface ValidateClass : NSObject
//验证邮箱
+ (BOOL)validateEmail:(NSString *)email;
//验证手机
+ (BOOL)validateMobile:(NSString *)mobile;
//验证车牌号
+ (BOOL)validateCarNo:(NSString *)carNo;
//验证用户名
+ (BOOL)validateUserName:(NSString *)name;
//验证密码
+ (BOOL)validatePassword:(NSString *)passWord;
//验证昵称
+ (BOOL)validateNickname:(NSString *)nickname;
//验证身份证
+ (BOOL)validateIdentityCard: (NSString *)identityCard;
//验证身份证号码最后一位是否为X 可用于身份证号码输入的自动补全
- (BOOL)theLastIsX:(NSString *)IDNumber;
//验证金额
+ (BOOL)validateMoney:(NSString *)money;
//验证webUrl
+ (BOOL)validateWebUrl:(NSString*)url;
//银行卡号验证
+ (BOOL)validateBankCard:(NSString*)cardNo;
//验证IP
+ (BOOL)isValidatIP:(NSString *)ipAddress;
@end
ValidateClass.m文件
//
// ValidateClass.m
// CustomProject
//
// Created by FengYiHao on 2017/7/20.
// Copyright © 2017年 FengYiHao. All rights reserved.
//
#import "ValidateClass.h"
@implementation ValidateClass
+ (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)validateMobile:(NSString *)mobile
{
//手机号以13, 15,18开头,八个 \d 数字字符
NSString *phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(17[^4,\\D])|(18[0,0-9]))\\d{8}$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//金额验证
+ (BOOL)validateMoney:(NSString *)money
{
NSString *phoneRegex = @"^([0-9]*[1-9][0-9]*)$";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:money];
}
//车牌号验证
+ (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)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)validateIdentityCard: (NSString *)identityCard
{
//判断是否为空
if (identityCard==nil||identityCard.length <= 0) {
return NO;
}
//判断是否是18位,末尾是否是x
NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
if(![identityCardPredicate evaluateWithObject:identityCard]){
return NO;
}
//判断生日是否合法
NSRange range = NSMakeRange(6,8);
NSString *datestr = [identityCard substringWithRange:range];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat : @"yyyyMMdd"];
if([formatter dateFromString:datestr]==nil){
return NO;
}
//判断校验位
if(identityCard.length==18)
{
NSArray *idCardWi= @[ @"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2" ]; //将前17位加权因子保存在数组里
NSArray * idCardY=@[ @"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2" ]; //这是除以11后,可能产生的11位余数、验证码,也保存成数组
int idCardWiSum=0; //用来保存前17位各自乖以加权因子后的总和
for(int i=0;i<17;i++){
idCardWiSum+=[[identityCard substringWithRange:NSMakeRange(i,1)] intValue]*[idCardWi[i] intValue];
}
int idCardMod=idCardWiSum%11;//计算出校验码所在数组的位置
NSString *idCardLast=[identityCard substringWithRange:NSMakeRange(17,1)];//得到最后一位身份证号码
//如果等于2,则说明校验码是10,身份证号码最后一位应该是X
if(idCardMod==2){
if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"]){
return YES;
}else{
return NO;
}
}else{
//用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
if([idCardLast intValue]==[idCardY[idCardMod] intValue]){
return YES;
}else{
return NO;
}
}
}
return NO;
}
/** theLastIsX
*
* 验证身份证号码最后一位是否为X 可用于身份证号码输入的自动补全
*
*/
- (BOOL)theLastIsX:(NSString *)IDNumber {
NSMutableArray *IDArray = [NSMutableArray array];
for (int i = 0; i < 17; i++) {
NSRange range = NSMakeRange(i, 1);
NSString *subString = [IDNumber substringWithRange:range];
[IDArray addObject:subString];
}
NSArray *coefficientArray = [NSArray arrayWithObjects:@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2", nil];
int sum = 0;
for (int i = 0; i < 17; i++) {
int coefficient = [coefficientArray[i] intValue];
int ID = [IDArray[i] intValue];
sum += coefficient * ID;
}
return (sum % 11 == 2) ? YES : NO;
}
//银行卡号验证
+ (BOOL)validateBankCard:(NSString*)cardNo{
int oddsum = 0; //奇数求和
int evensum = 0; //偶数求和
int allsum = 0;
int cardNoLength = (int)[cardNo length];
int lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];
cardNo = [cardNo substringToIndex:cardNoLength - 1];
for (int i = cardNoLength -1 ; i>=1;i--) {
NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1, 1)];
int tmpVal = [tmpString intValue];
if (cardNoLength % 2 ==1 ) {
if((i % 2) == 0){
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}else{
oddsum += tmpVal;
}
}else{
if((i % 2) == 1){
tmpVal *= 2;
if(tmpVal>=10)
tmpVal -= 9;
evensum += tmpVal;
}else{
oddsum += tmpVal;
}
}
}
allsum = oddsum + evensum;
allsum += lastNum;
if((allsum % 10) == 0)
return YES;
else
return NO;
}
//验证webUrl
+ (BOOL)validateWebUrl:(NSString*)url
{
NSString *urlRegex = @"((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|(www.[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)$";
NSPredicate *urlPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",urlRegex];
return [urlPredicate evaluateWithObject:urlRegex];
}
//验证IP
+ (BOOL)isValidatIP:(NSString *)ipAddress{
NSString *urlRegEx =@"^([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])\\."
"([01]?\\d\\d?|2[0-4]\\d|25[0-5])$";
NSError *error;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:urlRegEx options:0 error:&error];
if (regex != nil) {
NSTextCheckingResult *firstMatch=[regex firstMatchInString:ipAddress options:0 range:NSMakeRange(0, [ipAddress length])];
if (firstMatch) {
NSRange resultRange = [firstMatch rangeAtIndex:0];
NSString *result=[ipAddress substringWithRange:resultRange];
//输出结果
NSLog(@"%@",result);
return YES;
}
}
return NO;
}
@end