iOS|iOS 验证输入内容
前言
平时开发中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《》()[]{}【】^@/£¤¥|§¨「」『』¢¬ ̄~@#¥&*()——+|《》$_€。
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 = https://www.it610.com/article/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 = https://www.it610.com/article/YES;
}
}
} else if (substring.length> 1) {
const unichar ls = [substring characterAtIndex:1];
if (ls == 0x20e3) {
returnValue = https://www.it610.com/article/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;
}
总结 【iOS|iOS 验证输入内容】以上就是自己总结的常用正则和输入限制,后期会持续更新。。。
推荐阅读
- 2020-04-07vue中Axios的封装和API接口的管理
- Node.js中readline模块实现终端输入
- iOS中的Block
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密
- iOS,打Framework静态库