很多APP都会构建登录界面,我想分享我在搭建登录界面时用到的一些判断。
传送门:iOS开发 登录界面(界面版)
账号
判断用户的输入为邮箱?
/**
判断邮箱地址是否正确
@param email 邮箱账号
@return 结果
*/
+ (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];
}
判断用户的输入为手机号?
/**
判断手机号码格式是否正确
@param mobile 用户的手机号(字符串)
@return 结果
*/
+ (BOOL)valiMobile:(NSString *)mobile
{
//去掉所有的空格
mobile = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
if (mobile.length != 11)
{
return NO;
}else{
/**
* 移动号段正则表达式
*/
NSString *CM_NUM = @"^((13[4-9])|(147)|(15[0-2,7-9])|(178)|(18[2-4,7-8]))\\d{8}|(1705)\\d{7}$";
/**
* 联通号段正则表达式
*/
NSString *CU_NUM = @"^((13[0-2])|(145)|(15[5-6])|(176)|(18[5,6]))\\d{8}|(1709)\\d{7}$";
/**
* 电信号段正则表达式
*/
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;
}
}
}
判断用户的输入为身份证号?
/**
判断身份证号是否正确
@param )identityCard 身份证号
@return 结果
*
+ (BOOL) validateIdentityCard: (NSString *)identityCard{
//去掉所有的空格
identityCard = [identityCard stringByReplacingOccurrencesOfString:@" " withString:@""];
//身份证的位数为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;
}
}
判断账号是否同时包含字符串和数字
/**
判断字符串是否同时包含字母和数字
@param numAndChar 字符串
@return 结果
*/
-(BOOL)validateWithNumAndChar:(NSString *)numAndChar{
//去掉所有的空格
numAndChar = [numAndChar stringByReplacingOccurrencesOfString:@" " withString:@""];
//筛选数字条件
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){
//纯数字
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){
//包含字母和数字
return YES;
}else{
//包含字母和数字之外的其他字符
return NO;
}
}
网友评论