iOS正则表达式之验证手机号、邮箱、身份证、银行卡、密码6-20位数字和字母组合等等

释放双眼,带上耳机,听听看~!

 

1、邮箱验证:

+ (BOOL)isValidateEmail:(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];

}

2、密码验证:

+ (BOOL)isValidatePass:(NSString *)password

{

NSString *passRegex = @"[a-zA-Z0-9]{6,18}";

NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passRegex];;

return [passwordTest evaluateWithObject:password];

}

3、手机号验证:

+ (BOOL) isValidateMobile:(NSString *)mobile

{

//手机号以13, 15,18开头,八个 d 数字字符

NSString *phoneRegex = @"^[1][3,4,5,7,8]+\d{9}$";

NSPredicate *phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",phoneRegex];

return [phoneTest evaluateWithObject:mobile];

}

4、短信验证码验证:

+ (BOOL)isValidateVCode:(NSString *)vCode

{

NSString *vCodeRegex = @"[0-9]{6}";

NSPredicate *vCodeTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",vCodeRegex];;

return [vCodeTest evaluateWithObject:vCode];

}

5.身份证验证:

+ (BOOL) IsIdentityCard:(NSString *)IDCardNumber

{

if (IDCardNumber.length <= 0) 

{

return NO;

}

NSString *regex2 = @"^(\d{14}|\d{17})(\d|[xX])$";

NSPredicate *identityCardPredicate = [NSPredicate

predicateWithFormat:@"SELF MATCHES %@",regex2];

return [identityCardPredicate evaluateWithObject:IDCardNumber];

}

6、银行卡:

+ (BOOL) IsBankCard:(NSString *)cardNumber

{

if(cardNumber.length==0)

{

return NO;

}

NSString *digitsOnly = @"";

char c;

for (int i = 0; i < cardNumber.length; i++)

{

c = [cardNumber 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;

}

7、图片等比例压缩:

+(UIImage *) imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{

UIImage *newImage = nil;

CGSize imageSize = sourceImage.size;

CGFloat width = imageSize.width;

CGFloat height = imageSize.height;

CGFloat targetWidth = defineWidth;

CGFloat targetHeight = height / (width / targetWidth);

CGSize size = CGSizeMake(targetWidth, targetHeight);

CGFloat scaleFactor = 0.0;

CGFloat scaledWidth = targetWidth;

CGFloat scaledHeight = targetHeight;

CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);

if(CGSizeEqualToSize(imageSize, size) == NO){

CGFloat widthFactor = targetWidth / width;

CGFloat heightFactor = targetHeight / height;

if(widthFactor > heightFactor){

scaleFactor = widthFactor;

}

else{

scaleFactor = heightFactor;

}

scaledHeight = height * scaleFactor;

if(widthFactor > heightFactor){

thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;

}else if(widthFactor < heightFactor){

thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;

}

}

UIGraphicsBeginImageContext(size);

CGRect thumbnailRect = CGRectZero;

thumbnailRect.origin = thumbnailPoint;

thumbnailRect.size.width = scaledWidth;

thumbnailRect.size.height = scaledHeight;

[sourceImage drawInRect:thumbnailRect];

newImage = UIGraphicsGetImageFromCurrentImageContext();

if(newImage == nil){

NSLog(@"scale image fail");

}

UIGraphicsEndImageContext();

return newImage;

}

8、图片不等比例压缩:

+ (UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize

{

UIGraphicsBeginImageContext(newSize);

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];

UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return newImage;

}

9、获取通讯录(可以实现打电话):

+(NSMutableDictionary *)readAddr{

NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:0];

// Create addressbook data model

ABAddressBookRef addressBook;

if (&ABAddressBookCreateWithOptions != NULL)

{

CFErrorRef error = nil;

addressBook = ABAddressBookCreateWithOptions(NULL, &error);

//等待同意后向下执行

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {

addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

//等待同意后向下执行

dispatch_semaphore_t sema = dispatch_semaphore_create(0);

ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error){

dispatch_semaphore_signal(sema);

});

dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}

else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {

// The user has previously given access, add the contact

NSLog(@"通讯录已授权");

}

else {

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"系统提示" message:@"请在iPhone的“设置-隐私-通讯录”选项中,选择允许访问你的通讯录。" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

[alert show];

return nil;

// The user has previously denied access

// Send an alert telling user to change privacy setting in settings app

}

}

//取得本地通信录名柄

ABAddressBookRef tmpAddressBook = addressBook;

//取得本地所有联系人记录

NSArray* tmpPeoples = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(tmpAddressBook);

for(id tmpPerson in tmpPeoples){

//获得通讯录用户的电话

ABMultiValueRef tmpPhones = ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonPhoneProperty);

//判断电话数量是否大于0,没有电话的 就不要了

if (ABMultiValueGetCount(tmpPhones) > 0) {

//遍历用户电话

for(NSInteger j = 0; j < ABMultiValueGetCount(tmpPhones); j++){

//取出电话号码

NSString *tmpPhoneIndex = (__bridge NSString*)ABMultiValueCopyValueAtIndex(tmpPhones, j);

//将取出来的电话号码中的特殊字符替换为空字符串

NSString *phone;

phone = [tmpPhoneIndex stringByReplacingOccurrencesOfString:@"-" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@"(" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@")" withString:@""];

phone = [phone stringByReplacingOccurrencesOfString:@" " withString:@""];

//计算电话号码字符串中最后11位从哪1位开始

NSInteger phoneFromIndex = [phone length] - 11;

if (phoneFromIndex >= 0){

NSString *phoneStr = [phone substringFromIndex:phoneFromIndex];

//判断是否为手机号

if ([self isValidateMobile:phoneStr]) {

//获取的联系人单一属性:First name

NSString *tmpFirstName = (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonFirstNameProperty);

//获取的联系人单一属性:Last name

NSString *tmpLastName =  (__bridge NSString *)ABRecordCopyValue((__bridge ABRecordRef)(tmpPerson), kABPersonLastNameProperty);

//拼出用户的姓名,如果不判断是否=nil,显示出来就是null

NSString *userName;

if(tmpLastName && tmpFirstName){

userName = [NSString stringWithFormat:@"%@%@",tmpLastName,tmpFirstName];

}else{

if (tmpLastName) {

userName = tmpLastName;

}else if (tmpFirstName){

userName = tmpFirstName;

}else{

userName = @"";

}

}

[dict setValue:userName forKey:phoneStr];

}

}

}

}

CFRelease(tmpPhones);

}

//释放内存

CFRelease(tmpAddressBook);

return dict;

}

10、存到本地沙河:

+(NSString *)documentFilePath:(NSString *)fileName

{

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentDirectory = [paths objectAtIndex:0];

NSString *filePathStr = [documentDirectory stringByAppendingPathComponent:fileName];

return filePathStr;

}

11、正则匹配用户密码6-20位数字和字母组合:

+ (BOOL)checkPassword:(NSString *) password

{

NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";

NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];

BOOL isMatch = [pred evaluateWithObject:password];

return isMatch;

}

 

人已赞赏
iOS文章

iOS UIViewController的属性

2020-1-1 8:51:04

iOS文章

iOS UILabel根据字符串长度自动适应宽度和高度

2020-1-1 10:10:44

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索