//邮箱
+ (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];
}
//手机号码验证。YES 是手机号
+ (BOOL) validateMobile:(NSString *)mobile
{
//手机号以13,14, 15,17,18开头,八个 \d 数字字符
NSString *phoneRegex = @"(^1[3|4|5|7|8][0-9]{9}$)";
NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}
//用户名 判断是否包含汉字
+ (BOOL) validateUserName:(NSString *)name{
for(int i=0; i< [name length];i++){
int a = [name characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff){
return YES;
}
} return NO;
}
//身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard{
BOOL flag;
if (identityCard.length <= 0)
{
flag = NO;
return flag;
}
NSString *regex2 = @"^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$)|(^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[Xx])$)$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
flag = [identityCardPredicate evaluateWithObject:identityCard];
//如果通过该验证,说明身份证格式正确,但准确性还需计算
if(flag)
{
if(identityCard.length==18)
{
//将前17位加权因子保存在数组里
NSArray * idCardWiArray = @[@"7", @"9", @"10", @"5", @"8", @"4", @"2", @"1", @"6", @"3", @"7", @"9", @"10", @"5", @"8", @"4", @"2"];
//这是除以11后,可能产生的11位余数、验证码,也保存成数组
NSArray * idCardYArray = @[@"1", @"0", @"10", @"9", @"8", @"7", @"6", @"5", @"4", @"3", @"2"];
//用来保存前17位各自乖以加权因子后的总和
NSInteger idCardWiSum = 0;
for(int i = 0;i < 17;i++)
{
NSInteger subStrIndex = [[identityCard substringWithRange:NSMakeRange(i, 1)] integerValue];
NSInteger idCardWiIndex = [[idCardWiArray objectAtIndex:i] integerValue];
idCardWiSum+= subStrIndex * idCardWiIndex;
}
//计算出校验码所在数组的位置
NSInteger idCardMod=idCardWiSum%11;
//得到最后一位身份证号码
NSString * idCardLast= [identityCard substringWithRange:NSMakeRange(17, 1)];
//如果等于2,则说明校验码是10,身份证号码最后一位应该是X
if(idCardMod==2)
{
if([idCardLast isEqualToString:@"X"]||[idCardLast isEqualToString:@"x"])
{
return flag;
}else
{
flag = NO;
return flag;
}
}else
{
//用计算出的验证码与最后一位身份证号码匹配,如果一致,说明通过,否则是无效的身份证号码
if([idCardLast isEqualToString: [idCardYArray objectAtIndex:idCardMod]])
{
return flag;
}
else
{
flag = NO;
return flag;
}
}
}
else
{
flag = NO;
return flag;
}
}
else
{
return flag;
}
}
// 银行卡号
+ (BOOL) validateBankCard: (NSString *)bankCard
{
if (bankCard.length == 0) {
return NO;
}
NSString *digitsOnly = @"";
char c;
for (int i = 0; i < bankCard.length; i++)
{
c = [bankCard characterAtIndex:i];
if (isdigit(c))
{
digitsOnly =[digitsOnly stringByAppendingFormat:@"%c",c];
}
}
int sum = 0;
int digit = 0;
int addend = 0;
BOOL timesTwo = false;
for (NSInteger i = digitsOnly.length - 1; i >= 0; i--)
{
digit = [digitsOnly characterAtIndex:i] - '0';
if (timesTwo)
{
addend = digit * 2;
if (addend > 9) {
addend -= 9;
}
}
else {
addend = digit;
}
sum += addend;
timesTwo = !timesTwo;
}
int modulus = sum % 10;
return modulus == 0;
}
//获取手机IP
+ (NSString *)getPhoneIP {
NSString *address = @"error";
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
// retrieve the current interfaces - returns 0 on success
success = getifaddrs(&interfaces);
if (success == 0) {
// Loop through linked list of interfaces
temp_addr = interfaces;
while(temp_addr != NULL) {
if(temp_addr->ifa_addr->sa_family == AF_INET) {
// Check if interface is en0 which is the wifi connection on the iPhone
if([[NSString stringWithUTF8String:temp_addr->ifa_name] isEqualToString:@"en0"]) {
// Get NSString from C String
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
// Free memory
freeifaddrs(interfaces);
if ([address isEqualToString:@"error"])
{
address = @"192.168.1.1";
}
return address;
}
//第一次进App
+ (BOOL)isFirstTimeEnterApp
{
static NSString *fristTimeEnterAppKey = @"fristTimeEnterAppKey";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if([userDefaults objectForKey:fristTimeEnterAppKey])
{
return (NO);
}
[userDefaults setValue:@"" forKey:fristTimeEnterAppKey];
return (YES);
}
/**
* MD5 加密字符串
*/
+ (NSString *)MD5NSString:(NSString *)string{
if (string == nil || [string length] == 0) {
return nil;
}
unsigned char digest[CC_MD5_DIGEST_LENGTH], i;
CC_MD5([string UTF8String], (int)[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding], digest);
NSMutableString *ms = [NSMutableString string];
for (i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
[ms appendFormat:@"%02x", (int)(digest[i])];
}
return [ms copy];
}
转载于:https://www.cnblogs.com/darren-chen/p/5617997.html