iOS正則表達(dá)式之驗(yàn)證手機(jī)號(hào)致燥、郵箱登疗、身份證、銀行卡嫌蚤、密碼6-20位數(shù)字和字母組合等等

1辐益、郵箱驗(yàn)證:

+ (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、密碼驗(yàn)證:

+ (BOOL)isValidatePass:(NSString *)password

{

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

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

return [passwordTest evaluateWithObject:password];

}

3脱吱、手機(jī)號(hào)驗(yàn)證:

+ (BOOL) isValidateMobile:(NSString *)mobile

{

//手機(jī)號(hào)以13智政, 15,18開(kāi)頭箱蝠,八個(gè) \d 數(shù)字字符

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

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

return [phoneTest evaluateWithObject:mobile];

}

4续捂、短信驗(yàn)證碼驗(yàn)證:

+ (BOOL)isValidateVCode:(NSString *)vCode

{

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

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

return [vCodeTest evaluateWithObject:vCode];

}

5.身份證驗(yàn)證:

+ (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、獲取通訊錄(可以實(shí)現(xiàn)打電話):


+(NSMutableDictionary *)readAddr{

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

// Create addressbook data model

ABAddressBookRef addressBook;

if (&ABAddressBookCreateWithOptions != NULL)

{

CFErrorRef error = nil;

addressBook = ABAddressBookCreateWithOptions(NULL, &error);

//等待同意后向下執(zhí)行

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {

addressBook = ABAddressBookCreateWithOptions(NULL, NULL);

//等待同意后向下執(zhí)行

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(@"通訊錄已授權(quán)");

}

else {

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"系統(tǒng)提示" message:@"請(qǐng)?jiān)趇Phone的“設(shè)置-隱私-通訊錄”選項(xiàng)中间校,選擇允許訪問(wèn)你的通訊錄矾克。" 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;

//取得本地所有聯(lián)系人記錄

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

for(id tmpPerson in tmpPeoples){

//獲得通訊錄用戶的電話

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

//判斷電話數(shù)量是否大于0,沒(méi)有電話的 就不要了

if (ABMultiValueGetCount(tmpPhones) > 0) {

//遍歷用戶電話

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

//取出電話號(hào)碼

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

//將取出來(lái)的電話號(hào)碼中的特殊字符替換為空字符串

NSString *phone;

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

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

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

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

//計(jì)算電話號(hào)碼字符串中最后11位從哪1位開(kāi)始

NSInteger phoneFromIndex = [phone length] - 11;

if (phoneFromIndex >= 0){

NSString *phoneStr = [phone substringFromIndex:phoneFromIndex];

//判斷是否為手機(jī)號(hào)

if ([self isValidateMobile:phoneStr]) {

//獲取的聯(lián)系人單一屬性:First name

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

//獲取的聯(lián)系人單一屬性:Last name

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

//拼出用戶的姓名,如果不判斷是否=nil撇簿,顯示出來(lái)就是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);

}

//釋放內(nèi)存

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位數(shù)字和字母組合:

+ (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;

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末四瘫,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子欲逃,更是在濱河造成了極大的恐慌找蜜,老刑警劉巖,帶你破解...
    沈念sama閱讀 207,113評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件稳析,死亡現(xiàn)場(chǎng)離奇詭異洗做,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)彰居,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,644評(píng)論 2 381
  • 文/潘曉璐 我一進(jìn)店門(mén)诚纸,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人陈惰,你說(shuō)我怎么就攤上這事畦徘。” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 153,340評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵井辆,是天一觀的道長(zhǎng)关筒。 經(jīng)常有香客問(wèn)我,道長(zhǎng)杯缺,這世上最難降的妖魔是什么蒸播? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 55,449評(píng)論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮萍肆,結(jié)果婚禮上袍榆,老公的妹妹穿的比我還像新娘。我一直安慰自己塘揣,他們只是感情好蜡塌,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,445評(píng)論 5 374
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著勿负,像睡著了一般馏艾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上奴愉,一...
    開(kāi)封第一講書(shū)人閱讀 49,166評(píng)論 1 284
  • 那天琅摩,我揣著相機(jī)與錄音,去河邊找鬼锭硼。 笑死房资,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的檀头。 我是一名探鬼主播轰异,決...
    沈念sama閱讀 38,442評(píng)論 3 401
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼暑始!你這毒婦竟也來(lái)了搭独?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 37,105評(píng)論 0 261
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤廊镜,失蹤者是張志新(化名)和其女友劉穎牙肝,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體嗤朴,經(jīng)...
    沈念sama閱讀 43,601評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡配椭,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,066評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了雹姊。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片股缸。...
    茶點(diǎn)故事閱讀 38,161評(píng)論 1 334
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖吱雏,靈堂內(nèi)的尸體忽然破棺而出敦姻,到底是詐尸還是另有隱情寺酪,我是刑警寧澤,帶...
    沈念sama閱讀 33,792評(píng)論 4 323
  • 正文 年R本政府宣布替劈,位于F島的核電站寄雀,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏陨献。R本人自食惡果不足惜盒犹,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,351評(píng)論 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望眨业。 院中可真熱鬧急膀,春花似錦、人聲如沸龄捡。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,352評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)聘殖。三九已至晨雳,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間奸腺,已是汗流浹背餐禁。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,584評(píng)論 1 261
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留突照,地道東北人帮非。 一個(gè)月前我還...
    沈念sama閱讀 45,618評(píng)論 2 355
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像讹蘑,于是被迫代替她去往敵國(guó)和親末盔。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,916評(píng)論 2 344

推薦閱讀更多精彩內(nèi)容