前言
平时开发中textField比较常用,比如限制输入等。本文主要讨论textField相关代理方法于正则表达式的结合。
相关方法
/**
* 判断是否包含空格
*
* @param str NSString
*
* @return BOOL
*/
+ (BOOL)CheckisBlank:(NSString *)str{
NSRange _range = [str rangeOfString:@" "];
if (_range.location != NSNotFound) {
//有空格
return YES;
}else {
//没有空格
return NO;
}
}
/**
* 判断是否包含中文
*
* @param string NSString
*
* @return BOOL
*/
+ (BOOL)checkIsChinese:(NSString *)string{
for (int i=0; i<string.length; i++) {
unichar ch = [string characterAtIndex:i];
if (0x4E00 <= ch && ch <= 0x9FA5) {
return YES;
}
}
return NO;
}
/**
* 判断是否包含特殊字符
*
* @param string NSString
*
* @return BOOL
*/
+ (BOOL)isIncludeSpecialCharact: (NSString *)str {
//***需要过滤的特殊字符:~¥#&*<>《》()[]{}【】^@/£¤¥|§¨「」『』¢¬ ̄~@#¥&*()——+|《》$_€。
NSRange urgentRange = [str rangeOfCharacterFromSet: [NSCharacterSet characterSetWithCharactersInString: @"~¥#&*<>《》()[]{}【】^@/£¤¥|§¨「」『』¢¬ ̄~@#¥&*()——+|《》$_€"]];
if (urgentRange.location == NSNotFound)
{
return NO;
}
return YES;
}
/**
* 判断是否包含表情
*
* @param string NSString
*
* @return BOOL
*/
+ (BOOL)validateContainsEmoji:(NSString *)string {
__block BOOL returnValue = NO;
[string enumerateSubstringsInRange:NSMakeRange(0, [string length])
options:NSStringEnumerationByComposedCharacterSequences
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
const unichar hs = [substring characterAtIndex:0];
if (0xd800 <= hs && hs <= 0xdbff) {
if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
const int uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
if (0x1d000 <= uc && uc <= 0x1f77f) {
returnValue = YES;
}
}
} else if (substring.length > 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = YES;
}
} else {
if (0x2100 <= hs && hs <= 0x27ff) {
returnValue = YES;
} else if (0x2B05 <= hs && hs <= 0x2b07) {
returnValue = YES;
} else if (0x2934 <= hs && hs <= 0x2935) {
returnValue = YES;
} else if (0x3297 <= hs && hs <= 0x3299) {
returnValue = YES;
} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
returnValue = YES;
}
}
}];
return returnValue;
}
一些字符串验证方法
//验证手机号
+(BOOL)isMobileNumber:(NSString *)mobileNum{
NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
BOOL isMatch = [pred evaluateWithObject:mobileNum];
return isMatch;
}
//验证车牌号
+(BOOL)validateCarNo:(NSString*)carNo{
NSString *carRegex = @"^[A-Za-z]{1}[A-Za-z_0-9]{5}$";
NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
NSLog(@"carTest is %@",carTest);
return [carTest evaluateWithObject:carNo];
}
//正则匹配用户身份证号15或18位
+ (BOOL)checkUserIdCard: (NSString *) idCard{
NSString *pattern = @"(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:idCard];
return isMatch;
}
// 正则匹配URL
+ (BOOL)checkURL : (NSString *) url{
NSString *pattern = @"^[0-9A-Za-z]{1,50}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:url];
return isMatch;
}
//正则匹配用户密码6-18位数字和字母组合
+ (BOOL)checkPassword:(NSString *) password{
NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}";
NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
BOOL isMatch = [pred evaluateWithObject:password];
return isMatch;
}
总结
以上就是自己总结的常用正则和输入限制,后期会持续更新。。。
网友评论