很多APP都會(huì)構(gòu)建登錄界面懊直,我想分享我在搭建登錄界面時(shí)用到的一些判斷。
傳送門:iOS開(kāi)發(fā) 登錄界面(界面版)
賬號(hào)
判斷用戶的輸入為郵箱喉刘?
/**
判斷郵箱地址是否正確
@param email 郵箱賬號(hào)
@return 結(jié)果
*/
+ (BOOL) validateEmail:(NSString *)email{
//去掉所有的空格
email = [email stringByReplacingOccurrencesOfString:@" " withString:@""];
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];
}
判斷用戶的輸入為手機(jī)號(hào)歹叮?
/**
判斷手機(jī)號(hào)碼格式是否正確
@param mobile 用戶的手機(jī)號(hào)(字符串)
@return 結(jié)果
*/
+ (BOOL)valiMobile:(NSString *)mobile
{
//去掉所有的空格
mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
if (mobile.length != 11)
{
return NO;
}else{
/**
* 移動(dòng)號(hào)段正則表達(dá)式
*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 聯(lián)通號(hào)段正則表達(dá)式
*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 電信號(hào)段正則表達(dá)式
*/
NSString *CT_NUM = @"^((133)|(153)|(177)|(18[0,1,9]))\\d{8}$";
NSPredicate *pred1 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM_NUM];
BOOL isMatch1 = [pred1 evaluateWithObject:mobile];
NSPredicate *pred2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU_NUM];
BOOL isMatch2 = [pred2 evaluateWithObject:mobile];
NSPredicate *pred3 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT_NUM];
BOOL isMatch3 = [pred3 evaluateWithObject:mobile];
if (isMatch1 || isMatch2 || isMatch3) {
return YES;
}else{
return NO;
}
}
}
判斷用戶的輸入為身份證號(hào)?
/**
判斷身份證號(hào)是否正確
@param )identityCard 身份證號(hào)
@return 結(jié)果
*
+ (BOOL) validateIdentityCard: (NSString *)identityCard{
//去掉所有的空格
identityCard = [identityCard stringByReplacingOccurrencesOfString:@" " withString:@""];
//身份證的位數(shù)為18位
if (identityCard.length == 18) {
NSString *regex = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
return [identityCardPredicate evaluateWithObject:identityCard];
}else{
return NO;
}
}
判斷賬號(hào)是否同時(shí)包含字符串和數(shù)字
/**
判斷字符串是否同時(shí)包含字母和數(shù)字
@param numAndChar 字符串
@return 結(jié)果
*/
-(BOOL)validateWithNumAndChar:(NSString *)numAndChar{
//去掉所有的空格
numAndChar = [numAndChar stringByReplacingOccurrencesOfString:@" " withString:@""];
//篩選數(shù)字條件
NSRegularExpression *NumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger NumMatchCount = [NumRegularExpression numberOfMatchesInString:numAndChar options:NSMatchingReportProgress range:NSMakeRange(0, numAndChar.length)];
if(NumMatchCount == numAndChar.length){
//純數(shù)字
return NO;
}
//篩選字母條件
NSRegularExpression *LetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Za-z]" options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger LetterMatchCount = [LetterRegularExpression numberOfMatchesInString:numAndChar options:NSMatchingReportProgress range:NSMakeRange(0, numAndChar.length)];
if(LetterMatchCount == numAndChar.length){
//純字母
return NO;
}
if(NumMatchCount + LetterMatchCount == numAndChar.length){
//包含字母和數(shù)字
return YES;
}else{
//包含字母和數(shù)字之外的其他字符
return NO;
}
}