在iOS开发中,经常有用到对输入的字符串进行判断。总结如下。
(1)有效的电话号码
- (BOOL) isValidMobileNumber {
NSString* const MOBILE = @"^1(3|4|5|7|8)\\d{9}$";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
return [predicate evaluateWithObject:self];
}
(2)有效的验证码(根据自家的验证码位数进行修改)
- (BOOL) isValidVerifyCode{
NSString *pattern = @"^[0-9]{4}";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
return [predicate evaluateWithObject:self];
}
(3)有效的真实姓名
- (BOOL) isValidRealName{
NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{2,8}$";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
return [predicate evaluateWithObject:self];
}
(4)是否只有中文
- (BOOL) isOnlyChinese{
NSString * chineseTest=@"^[\u4e00-\u9fa5]{0,}$";
NSPredicate*chinesePredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",chineseTest];
return [chinesePredicate evaluateWithObject:self];
}
(5)有效的银行卡号
- (BOOL) isValidBankCardNumber {
NSString* const BANKCARD = @"^(\\d{16}|\\d{19})$";
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", BANKCARD];
return [predicate evaluateWithObject:self];
}
(6)有效的邮箱
- (BOOL) isValidEmail{
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:self];
}
(7)昵称验证
- (BOOL) validateNickName{
NSString *userNameRegex = @"^[A-Za-z0-9\u4e00-\u9fa5]{1,24}+$";
NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
return [userNamePredicate evaluateWithObject:self];
}
(8)有效的字母数字密码
- (BOOL) isValidAlphaNumberPassword{
NSString *regex = @"^(?!\\d+$|[a-zA-Z]+$)\\w{6,12}$";
NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [identityCardPredicate evaluateWithObject:self];
}
(9)检测有效身份证 (15位)
- (BOOL) isValidIdentifyFifteen{
NSString * identifyTest=@"^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$";
NSPredicate*identifyPredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",identifyTest];
return [identifyPredicate evaluateWithObject:self];
}
(10)检测有效身份证(18位)
- (BOOL) isValidIdentifyEighteen{
NSString * identifyTest=@"^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)$";
NSPredicate*identifyPredicate=[NSPredicate predicateWithFormat:@"SELF MATCHES %@",identifyTest];
return [identifyPredicate evaluateWithObject:self];
}
(11)限制只能输入数字
- (BOOL) isOnlyNumber{
BOOL res = YES;
NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
int i = 0;
while (i < self.length) {
NSString * string = [self substringWithRange:NSMakeRange(i, 1)];
NSRange range = [string rangeOfCharacterFromSet:tmpSet];
if (range.length == 0) {
res = NO;
break;
}
i++;
}
return res;
}
(19)限制只能输入数字和小数点且小数点后保留两位
- (BOOL) isOnlyNumberCashRange:(NSRange)range{
NSString * cash= (NSString*)self;
NSScanner *scanner = [NSScanner scannerWithString:cash];
NSCharacterSet *numbers;
NSRange pointRange = [self rangeOfString:@"."];
if ( (pointRange.length > 0) && (pointRange.location < range.location || pointRange.location > range.location + range.length) )
{
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
}
else
{
numbers = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
}
if ( [self isEqualToString:@""] && [cash isEqualToString:@"."] )
{
return NO;
}
if (range.location == 1) {
if ([cash isEqualToString:@"."]) {
return NO;
}
}
short remain = 2; //默认保留2位小数
NSString *tempStr = [self stringByAppendingString:cash];
NSUInteger strlen = [tempStr length];
if(pointRange.length > 0 && pointRange.location > 0){ //判断输入框内是否含有“.”。
if([cash isEqualToString:@"."]){ //当输入框内已经含有“.”时,如果再输入“.”则被视为无效。
return NO;
}
if(strlen > 0 && (strlen - pointRange.location) > remain+1){ //当输入框内已经含有“.”,当字符串长度减去小数点前面的字符串长度大于需要要保留的小数点位数,则视当次输入无效。
return NO;
}
}else{
if (range.location>4) {
if (![cash isEqualToString:@"."]) {
return NO;
}
}
}
NSRange zeroRange = [self rangeOfString:@"0"];
if(zeroRange.length == 1 && zeroRange.location == 0){ //判断输入框第一个字符是否为“0”
if(![cash isEqualToString:@"0"] && ![cash isEqualToString:@"."] && [self length] == 1)
{ //当输入框只有一个字符并且字符为“0”时,再输入不为“0”或者“.”的字符时,则将此输入替换输入框的这唯一字符。
return NO;
}
else
{
if(pointRange.length == 0 && pointRange.location > 0){ //当输入框第一个字符为“0”时,并且没有“.”字符时,如果当此输入的字符为“0”,则视当此输入无效。
if([cash isEqualToString:@"0"]){
return NO;
}
}
}
}
NSString *buffer;
if ( ![scanner scanCharactersFromSet:numbers intoString:&buffer] && ([cash length] != 0) )
{
return NO;
}
return YES;
}
网友评论