1、代码实现"密码至少为9位,并需包含大写字母、小写字母、数字或特殊字符等三种"

返回0、1、2为格式不正确,返回4为密码格式正确

-(int)checkIsHaveNumAndLetter:(NSString*)password

{

//数字条件

NSRegularExpression *tNumRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[0-9]" options:NSRegularExpressionCaseInsensitive error:nil];

//符合数字条件的有几个字节

NSUInteger tNumMatchCount = [tNumRegularExpression numberOfMatchesInString:password

options:NSMatchingReportProgress

range:NSMakeRange(0, password.length)];

//英文字条件

NSRegularExpression *sLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[a-z]" options:NSRegularExpressionDotMatchesLineSeparators error:nil];

//符合英文字条件的有几个字节

NSUInteger sLetterMatchCount = [sLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

//英文字条件

NSRegularExpression *tLetterRegularExpression = [NSRegularExpression regularExpressionWithPattern:@"[A-Z]" options:NSRegularExpressionDotMatchesLineSeparators error:nil];

//符合英文字条件的有几个字节

NSUInteger tLetterMatchCount = [tLetterRegularExpression numberOfMatchesInString:password options:NSMatchingReportProgress range:NSMakeRange(0, password.length)];

if (password.length < 9) {

// 密码长度不正确

return 0;

} else {

// 没有大写或小写

if (tLetterMatchCount == 0 || sLetterMatchCount == 0) {

return 1;

} else {

if (tNumMatchCount > 0) {

return 4;

} else{

if(tNumMatchCount + tLetterMatchCount + sLetterMatchCount < password.length){

return 4;

} else{

return 2;

}

}

}

}

}

需注意:NSRegularExpressionOptions,如果不区分大小写可以使用 NSRegularExpressionCaseInsensitive

NSRegularExpressionCaseInsensitive		  = 1 << 0,   // 不区分大小写的
NSRegularExpressionAllowCommentsAndWhitespace = 1 << 1, // 忽略空格和# -
NSRegularExpressionIgnoreMetacharacters = 1 << 2, // 整体化
NSRegularExpressionDotMatchesLineSeparators = 1 << 3, // 匹配任何字符,包括行分隔符
NSRegularExpressionAnchorsMatchLines = 1 << 4, // 允许^和$在匹配的开始和结束行
NSRegularExpressionUseUnixLineSeparators = 1 << 5, // (查找范围为整个的话无效)
NSRegularExpressionUseUnicodeWordBoundaries = 1 << 6 // (查找范围为整个的话无效)

iOS使用正则匹配限制输入密码格式的更多相关文章

  1. linux 下正则匹配时间命名格式的文件夹

    用正则表达式匹配时间格式命名的文件夹 ls mypath | grep -E "[0-9]{4}-[0-9]{1,2}" mypath为需要查询的目录 查询出来的文件夹格式为:例 ...

  2. js正则匹配以固定格式结尾的字符串并匹配是手机访问,则跳转

    <script> //var pcUrl = "http://res.meadin.com/HotelData/98986_1.shtml"; var pcUrl = ...

  3. 【php正则】php正则匹配UTF-8格式的中文汉字 和 【,】【,】【。】等符号

    1.php正则匹配UTF-8格式的中文汉字 和 [,][,][.]等符号 if (preg_match_all("/([\x{4e00}-\x{9fa5}]+((,)?)+((,)?)+(( ...

  4. iOS正则匹配手机号

    #pragma 正则匹配手机号 + (BOOL)validateMobile:(NSString *)mobileNum {     /**      * 手机号码      * 移动:134[0-8 ...

  5. iOS 中的正则匹配(工具类方法)

    正则表达式 正则表达式是对字符串操作的一种逻辑公式, 用事先定义好的一些特定字符.及这些特定字符的组合, 组成一个"规则字符串", 这个"规则字符串"用来表达对 ...

  6. python_way day18 html-day4, Django路由,(正则匹配页码,包含自开发分页功能), 模板, Model(jDango-ORM) : SQLite,数据库时间字段插入的方法

    python_way day18 html-day4 1.Django-路由系统   - 自开发分页功能 2.模板语言:之母板的使用 3.SQLite:model(jDango-ORM) 数据库时间字 ...

  7. 常用的JavaScript正则匹配规则代码收藏,很实用

    收集一些常用的JavaScript正则表达式匹配规则,比如匹配电话号码.Email.中文字符.身份证号.邮编.QQ号.过滤空白行.匹配特定数字等.觉得这玩意是很有用的,只不过自己水平菜,老是自己写不出 ...

  8. 正则匹配<img src="xxxxxx" alt="" />标签的相关写法

    1.(<img\ssrc[^>]*>) 2.content.replace(/<img [^>]*src=['"]([^'"]+)[^>]*&g ...

  9. Saltstack 操作目标,正则匹配,及组管理

    如果我们要维护好一个庞大的配置管理系统那么首选得维护好我们的管理对象,在saltstack系统中我们的管理对象叫做Target, 在master上我们可以采用不同Target去管理不同的Minion. ...

随机推荐

  1. HTTP请求和响应2:方法(Method)

    方法表明了client希望server对资源运行的动作.经常使用的方法包含:GET.HEAD.POST.PUT.TRACE.OPTIONS和DELETE,每一个server能够实现这些方法中的部分或者 ...

  2. Shader toy (顺手写两个Gyro)(纯代码写3D)

    Shader toy (A new world) 1.一个多月了,突然忘记CSDN的password了.由于每次输password的时候都要计算一遍,于是没有计算出来- 2.回头发现都过了半年了,都快 ...

  3. javaweb项目自定义错误页面

    当我们把一个web项目成功发布出去,但是有些页面还有待完善的时候,会出现404错误页面.这个会给用户很差的体验.如何将这些错误页面修改为自定义的错误页界面,给用户一些友好的提示呢? 首先我们在web. ...

  4. UI自动化测试篇 :Selenium2(Webdriver)&TestNG自动化测试环境搭建

    最开始学习UI自动化,用的工具是QTP10,用起来确实比较容易上手,自学了没多久,大家都说QTP过时了.这么好用的的工具怎么一下子就过时了呢?因为它的“笨重”,因为它作为商业软件带来的巨大使用成本,还 ...

  5. python可hash 不可hash类型

    不可变类型是可hash #tuple str freezeset 可变类型是不可hash ##list set

  6. quick-cocos2d-x transition使用方法

    Functions transition.newEasing(action, easingName, more) 为图像创造效果 transition.execute(target, action,  ...

  7. linux 下mtime,ctime,atime分析

    一.atime.ctime与mtime atime是指access time,即文件被读取或者执行的时间,修改文件是不会改变access time的.网上很多资料都声称cat.more等读取文件的命令 ...

  8. 写sql语句统计各个学生各科成绩(case when用法)

    尊重原创:http://blog.csdn.net/love_java_cc/article/details/78268326 有如下一张表score: 建表语句: CREATE TABLE `sco ...

  9. MySQL_使用时遇到的问题汇总

    一.data too long for column 'name' at row 1 1.现象:把数据库的字符集编码设置为utf-8,通过DOS界面向表的某一列插入汉字时会遇到类似 data too ...

  10. web.xml中url-pattern匹配规则

    小知识 一般的URL组成 URL = 服务器地址 + RequestURI 例如URI:http://localhost:8080/practice/main [http://localhost:80 ...