ios 判断,qq,银行卡,手机号,邮编,生日,数字,字符串,护照, email
http://blog.csdn.net/dyllove98/article/details/8635079
IdentifierValidator.h
//
// IdentifierValidator.h
// 银行卡校验
//
// Created by 杨梦晓 on 14-9-9.
// Copyright (c) 2014年 杨梦晓. All rights reserved.
//
#import <Foundation/Foundation.h>
typedefenum
{
IdentifierTypeKnown = ,
IdentifierTypeZipCode, //1
IdentifierTypeEmail, //2
IdentifierTypePhone, //3
IdentifierTypeUnicomPhone, //4
IdentifierTypeQQ, //5
IdentifierTypeNumber, //6
IdentifierTypeString, //7
IdentifierTypeIdentifier, //8
IdentifierTypePassort, //9
IdentifierTypeCreditNumber, //10
IdentifierTypeBirthday, //11
}IdentifierType;
@interface IdentifierValidator : NSObject
+ (BOOL) isValid:(IdentifierType) type value:(NSString*) value;
@end
-----------------------------------------------
IdentifierValidator.m
//
// IdentifierValidator.m
// 银行卡校验
//
// Created by 杨梦晓 on 14-9-9.
// Copyright (c) 2014年 杨梦晓. All rights reserved.
//
#import "IdentifierValidator.h"
#import "NSString+ITTAdditions.h"
int getIndex (char ch);
BOOL isNumber (char ch);
int getIndex (char ch) {
if ((ch >= '0'&& ch <= '9')||(ch >= 'a'&& ch <= 'z')||
(ch >= 'A' && ch <= 'Z')|| ch == '_') {
;
}
if (ch == '@') {
;
}
if (ch == '.') {
;
}
;
}
BOOL isNumber (char ch)
{
if (!(ch >= '0' && ch <= '9')) {
returnFALSE;
}
returnTRUE;
}
@implementation IdentifierValidator
#pragma mark - 校验邮编
+ (BOOL) isValidZipcode:(NSString*)value
{
const char *cvalue = [value UTF8String];
int len = strlen(cvalue);
) {
returnFALSE;
}
; i < len; i++)
{
if (!(cvalue[i] >= '0' && cvalue[i] <= '9'))
{
return FALSE;
}
}
returnTRUE;
}
#pragma mark - 校验email
+ (BOOL) validateEmail:(NSString *)candidate
{
NSArray *array = [candidate componentsSeparatedByString:@"."];
) {
returnFALSE;
}
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:candidate];
}
+ (BOOL) isValidEmail:(NSString*)value {
][] = {
{, -, -},
{, , -},
{, -, -},
{, -, },
{, -, -}
};
BOOL valid = TRUE;
const char *cvalue = [value UTF8String];
;
int len = strlen(cvalue);
int index;
; i < len && valid; i++) {
index = getIndex(cvalue[i]);
) {
valid = FALSE;
}
else {
currentState = state[currentState][index];
) {
valid = FALSE;
}
}
}
//end state is invalid
) {
valid = FALSE;
}
return valid;
}
#pragma mark - 校验数字
+ (BOOL) isValidNumber:(NSString*)value{
const char *cvalue = [value UTF8String];
int len = strlen(cvalue);
; i < len; i++) {
if(!isNumber(cvalue[i])){
return FALSE;
}
}
returnTRUE;
}
#pragma mark - 校验手机号
+ (BOOL) isValidPhone:(NSString*)value {
const char *cvalue = [value UTF8String];
int len = strlen(cvalue);
) {
returnFALSE;
}
if (![IdentifierValidatorisValidNumber:value])
{
returnFALSE;
}
];
if ([preString isEqualToString:@"13"] ||
[preString isEqualToString: @"15"] ||
[preString isEqualToString: @"18"])
{
return TRUE;
}
else
{
returnFALSE;
}
returnTRUE;
}
#pragma mark - 校验字符串
+ (BOOL) isValidString:(NSString*)value
{
return value && [value length];
}
, , , , , , , , , , , , , , , , };//加权因子
, , , , , , , , , , };//校验值对应表
#pragma mark - 校验身份证号
+ (BOOL) isValidIdentifier:(NSString*)value
{
;
const char *str = [[value lowercaseString] UTF8String];
NSInteger i;
NSInteger length = strlen(str);
BOOL result = TRUE;
/*
* identifier length is invalid
*/
!= length && LENGTH != length)
{
result = FALSE;
}
else
{
; i < length - ; i++)
{
if(!(str[i] >= '0' && str[i] <= '9'))
{
result = FALSE;
break;
}
}
if (result)
{
if(LENGTH == length)
{
if (!((str[i] >= '0' && str[i] <= '9')||str[i] == 'X'||str[i] == 'x'))
{
result = FALSE;
}
}
}
/*
* check sum for second generation identifier
*/
if (result && length == LENGTH)
{
int i;
int *ids = malloc(sizeof(int)*LENGTH);
; i < LENGTH; i++)
{
ids[i] = str[i] - ;
}
;
; i < LENGTH - ; i ++ )
{
checksum += ids[i] * factor[i];
}
] == ]||
(str[] == ] == ))
{
result = TRUE;
}
else
{
result = FALSE;
}
free(ids);
ids = NULL;
}
}
return result;
}
#pragma mark - 校验护照
+ (BOOL) isValidPassport:(NSString*)value
{
const char *str = [value UTF8String];
];
NSInteger length = strlen(str);
if (!(first == 'P' || first == 'G'))
{
returnFALSE;
}
if (first == 'P')
{
)
{
return FALSE;
}
}
if (first == 'G')
{
)
{
return FALSE;
}
}
BOOL result = TRUE;
; i < length; i++)
{
if (!(str[i] >= '0' && str[i] <= '9'))
{
result = FALSE;
break;
}
}
return result;
}
/*
* 常用信用卡卡号规则
* Issuer Identifier Card Number Length
* Diner's Club 300xxx-305xxx, 3095xx, 36xxxx, 38xxxx 14
* American Express 34xxxx, 37xxxx 15
* VISA 4xxxxx 13, 16
* MasterCard 51xxxx-55xxxx 16
* JCB 3528xx-358xxx 16
* Discover 6011xx 16
* 银联 622126-622925 16
*
* 信用卡号验证基本算法:
* 偶数位卡号奇数位上数字*2,奇数位卡号偶数位上数字*2。
* 大于10的位数减9。
* 全部数字加起来。
* 结果不是10的倍数的卡号非法。
* prefrences link:http://www.truevue.org/licai/credit-card-no
*
*/
#pragma mark - 校验信用卡号
+ (BOOL) isValidCreditNumber:(NSString*)value
{
BOOL result = TRUE;
NSInteger length = [value length];
)
{
result = [IdentifierValidator isValidNumber:value];
if (result)
{
, )] integerValue];
, )] integerValue];
, )] integerValue];
//Diner's Club
&& threeDigitBeginValue <= )||
fourDigitBeginValue == ||twoDigitBeginValue==||twoDigitBeginValue==) && ( != length))
{
result = FALSE;
}
//VISA
== length|| == length))
{
result = FALSE;
}
//MasterCard
||twoDigitBeginValue <= ) && ( != length))
{
result = FALSE;
}
//American Express
!= length))
{
result = FALSE;
}
//Discover
!= length))
{
result = FALSE;
}
else
{
, )] integerValue];
//CUP
&& begin <= ) && ( != length))
{
result = FALSE;
}
//other
else
{
result = TRUE;
}
}
}
if (result)
{
NSInteger digitValue;
;
;
NSInteger leftIndex;
//even length, odd index
== length%)
{
index = ;
leftIndex = ;
}
//odd length, even index
else
{
index = ;
leftIndex = ;
}
while (index < length)
{
digitValue = [[value )] integerValue];
digitValue = digitValue*;
)
{
checkSum += digitValue/ + digitValue%;
}
else
{
checkSum += digitValue;
}
digitValue = [[value )] integerValue];
checkSum += digitValue;
index += ;
leftIndex += ;
}
result = ( == checkSum%) ? TRUE:FALSE;
}
}
else
{
result = FALSE;
}
return result;
}
#pragma mark - 校验生日
+ (BOOL) isValidBirthday:(NSString*)birthday
{
BOOL result = FALSE;
== [birthday length])
{
NSDateFormatter *formatter = [[NSDateFormatteralloc] init];
[formatter setDateFormat:@"yyyyMMdd"];
NSDate *date = [formatter dateFromString:birthday];
if (date)
{
result = TRUE;
}
}
return result;
}
#pragma mark - 校验联通号
+ (BOOL) isChinaUnicomPhoneNumber:(NSString*) phonenumber
{
/**
* 手机号码
* 移动: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
*/
// NSString * MOBILE = @"^1(3[0-9]|5[0-35-9]|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-8]|(3[5-9]|5[017-9]|8[278])\\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[09])[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 * PHS1 = @"^0(10|2[0-5789]|\\d{3}-)\\d{7,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 *regextestphs = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS];
// NSPredicate *regextestphs1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", PHS1];
if (//([regextestmobile evaluateWithObject:phonenumber] == YES)||
// ([regextestcm evaluateWithObject:phonenumber] == YES)||
// ([regextestct evaluateWithObject:phonenumber] == YES)||
([regextestcu evaluateWithObject:phonenumber] == YES)
// || ([regextestphs evaluateWithObject:phonenumber] == YES)
// || ([regextestphs1 evaluateWithObject:phonenumber] == YES)
)
{
return YES;
}
else
{
return NO;
}
}
//+ (BOOL) isChinaUnicomPhoneNumber:(NSString*) phoneNumber
//{
// BOOL unicom = TRUE;
// NSString *mobileNumFormat13 = @"[1]{1}[3]{1}[4-9]{1}[0-9]{8}";
// NSString *mobileNumFormat14 = @"[1]{1}[4]{1}[7]{1}[0-9]{8}";
// NSString *mobileNumFormat15 = @"[1]{1}[5]{1}[012789]{1}[0-9]{8}";
// NSString *mobileNumFormat18 = @"[1]{1}[8]{1}[2378]{1}[0-9]{8}";
// NSPredicate *predicate13 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat13];
// NSPredicate *predicate14 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat14];
// NSPredicate *predicate15 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat15];
// NSPredicate *predicate18 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",mobileNumFormat18];
// if ([predicate13 evaluateWithObject:phoneNumber] ||
// [predicate14 evaluateWithObject:phoneNumber] ||
// [predicate15 evaluateWithObject:phoneNumber] ||
// [predicate18 evaluateWithObject:phoneNumber])
// {
// unicom = FALSE;
// }
// return unicom;
//}
+ (BOOL) isValid:(IdentifierType) type value:(NSString*) value
{
if (!value ||[[value stringByTrimmingCharactersInSet:[NSCharacterSetwhitespaceCharacterSet]] isEqualToString:@""]) {
returnFALSE;
}
BOOL result = TRUE;
switch (type)
{
caseIdentifierTypeZipCode:
result = [IdentifierValidator isValidZipcode:value];
break;
caseIdentifierTypeEmail:
// result = [IdentifierValidator isValidEmail:value];
result = [IdentifierValidator validateEmail:value];
break;
caseIdentifierTypePhone:
result = [IdentifierValidator isValidPhone:value];
break;
caseIdentifierTypeUnicomPhone:
result = [IdentifierValidator isChinaUnicomPhoneNumber:value];
break;
caseIdentifierTypeQQ:
result = [IdentifierValidator isValidNumber:value];
break;
caseIdentifierTypeNumber:
result = [IdentifierValidator isValidNumber:value];
break;
caseIdentifierTypeString:
result = [IdentifierValidator isValidString:value];
break;
caseIdentifierTypeIdentifier:
result = [IdentifierValidator isValidIdentifier:value];
break;
caseIdentifierTypePassort:
result = [IdentifierValidator isValidPassport:value];
break;
caseIdentifierTypeCreditNumber:
result = [IdentifierValidator isValidCreditNumber:value];
break;
caseIdentifierTypeBirthday:
result = [IdentifierValidator isValidBirthday:value];
break;
default:
break;
}
return result;
}
@end
-----------------------------------------------
NSString+ITTAdditions.h
//
// NSString+ITTAdditions.h
// 银行卡校验
//
// Created by 杨梦晓 on 14-9-9.
// Copyright (c) 2014年 杨梦晓. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSString (ITTAdditions)
- (BOOL)isStartWithString:(NSString *)string;
@end
-----------------------------------------------
NSString+ITTAdditions.m
//
// NSString+ITTAdditions.m
// 银行卡校验
//
// Created by 杨梦晓 on 14-9-9.
// Copyright (c) 2014年 杨梦晓. All rights reserved.
//
#import "NSString+ITTAdditions.h"
@implementation NSString (ITTAdditions)
- (BOOL)isStartWithString:(NSString *)string{
int temp = [string intValue];
switch (temp) {
:
return NO;
break;
:
return NO;
break;
:
return NO;
break;
:
return NO;
break;
default:
break;
}
returnYES;
}
@end
-----------------------------------------------
MXViewController.m
//
// MXViewController.m
// 信用卡校验
//
// Created by 杨梦晓 on 14-9-9.
// Copyright (c) 2014年 ___FULLUSERNAME___. All rights reserved.
//
#import "MXViewController.h"
#import "IdentifierValidator.h"
@interfaceMXViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutletUITextField *textField;
@end
@implementation MXViewController
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
// 信用卡校验
if ([IdentifierValidatorisValid:IdentifierTypeCreditNumbervalue:textField.text]) {
NSLog(@"通过");
}else{
NSLog(@"不通过");
}
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[[[UIApplicationsharedApplication] keyWindow] endEditing:YES];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSString *text = [textField text];
NSCharacterSet *characterSet = [NSCharacterSetcharacterSetWithCharactersInString:@"0123456789\b"];
string = [string stringByReplacingOccurrencesOfString:@" "withString:@""];
if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) {
return NO;
}
text = [text stringByReplacingCharactersInRange:range withString:string];
text = [text stringByReplacingOccurrencesOfString:@" "withString:@""];
NSString *newString = @"";
) {
)];
newString = [newString stringByAppendingString:subString];
) {
newString = [newString stringByAppendingString:@" "];
}
text = [text )];
}
newString = [newString stringByTrimmingCharactersInSet:[characterSet invertedSet]];
) {
return NO;
}
[textField setText:newString];
returnNO;
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
ios 判断,qq,银行卡,手机号,邮编,生日,数字,字符串,护照, email的更多相关文章
- php通过gbk编码判断 含有连续数字 可用于判断QQ号,手机号等。
有可能有些输入,不希望让用户的评论或者私信中含有类似于QQ号,手机号的文本,比如交友网站.还有些恶意SEO通过,构造恶意检索词,检索词中包含QQ,手机号等,让百度爬取到,增加展现.也需要将这些检索词屏 ...
- C#判断字符串是否为数字字符串
在进行C#编程时候,有的时候我们需要判断一个字符串是否是数字字符串,我们可以通过以下两种方法来实现.[方法一]:使用 try{} catch{} 语句. 我们可以在try语句块中试图将str ...
- iOS开发之---判断是否是手机号
iOS开发之---判断是否是手机号
- iOS开发,最新判断是否是手机号的正则表达式
最近项目里需要判断是否为手机号并发送验证码的功能,一下是实现方法.不过这个方法还是有些不足,只能判断输入的11位数的号段是否正确,无法判断手机号是否存在.不过勉强可以使用! + (NSString * ...
- iOS中判断基础字符(大小写、数字等的判断)
函数:isdigit 用法:#include 功能:判断字符c是否为数字 说明:当c为数字0-9时,返回非零值,否则返回零. 函数:islower 用法:#include 功能:判断字符c是否为小写英 ...
- IOS 开发qq登陆界面
// // ViewController.m // QQUI_bydfg // // Created by Kevin_dfg on 16/4/15. // Copyright © 2016年 ...
- iOS开发 QQ粘性动画效果
QQ(iOS)客户端的粘性动画效果 时间 2016-02-17 16:50:00 博客园精华区 原文 http://www.cnblogs.com/ziyi--caolu/p/5195615.ht ...
- 微信、QQ和手机号之间不得不说的故事!
发文字,发图片,发心情,视频聊天,查看附近的人,微信能干的事情QQ都可以,那么它们有什么区别,我QQ用得好好的为什么要我联系人都导到微信去?我们很早就有了QQ,但是在QQ时代,我们虽然用QQ发消息聊天 ...
- iOS调用QQ发起临时会话
iOS调用QQ发起临时会话 iOS调用qq前先判断是否安装qq, 之后通过OpenURL打开对用的qq NSURL *url = [NSURL URLWithString:@"mqq://& ...
- 判断qq浏览器和uc浏览器?
判断在iphone手机上打开的是uc浏览器还是qq浏览器 <html lang="en"> <head> <meta charset="ut ...
随机推荐
- android——manifest.xml
- my.ini配置
在家里写点东西,需要配置mysql windows版本,linux版本我一般都是直接编译后,有模板文件可以编辑,新下载的5.6没有,我用的是zip的. 在网上找了两篇博客,写的很详细: http:// ...
- HDU 3589 Jacobi symbol
彻底对数学绝望了 #include <cstdio> #include <cmath> int flag[1005],p[500],a; int d[100]; int ini ...
- java实现字符串匹配问题之求两个字符串的最大公共子串
转载请注明出处:http://blog.csdn.net/xiaojimanman/article/details/38924981 近期在项目工作中有一个关于文本对照的需求,经过这段时间的学习,总结 ...
- readv和writev函数
readv 和 writev 函数用于在一次函数调用中读.写多个非连续缓冲区.有时也将这两个函数称为散布读和聚集写. #include <sys/uio.h> ssize_t readv( ...
- Android中图片处理相关问题
在Android的开发中,我们经常回去处理一些图片相关的问题,比如当加载图片到内存中产生的OOM(OutOfMemory)异常.图片加载到内存中占多大内存的问题.jpg png两种常见的图片的原理及区 ...
- 融合python2和python3
很多情况下你可能会想要开发一个程序能同时在python2和python3中运行. 想象一下你开发了一个模块,成百上千的人都在使用它,但不是所有的用户都同时使用python 2和3.这种情况下你有两个选 ...
- VMware: linux起步提示 memory for crashkernel(0*0 to 0*0)not within permissible
(virtualbox/VMware)linux起步提示memoryforcrashkernel(0*0 to 0*0)notwithinpermissible http://www.myexcep ...
- [置顶] 程序员必知(二):位图(bitmap)
位图是什么? 位图就是数组,一般来说是bit型的数组,具有快速定位某个值的功能,这种思想有很广泛的应用,比如下边两题: 1 找出一个不在5TB个整数中存在的数 假设整数是32位的,总共有4GB个数,我 ...
- HTML解决div里面img的缝隙问题
图片IMG与容器下边界之间有空隙怎么办?这里介绍3中简单的解决方法. 第一,给图片img标签display:block. img{display:block} 第二,定义容器里的字体大小为0. div ...