Swift 常用NSPredicate

HK number 验证

判断以8位数为标准的695807开头的number

static func isHKphone(phone:String) -> Bool {
       let hkphone = "^(6|9|5|8|0|7)\\d{7}$"
       let regexMobile = NSPredicate(format:"SELF MATCHES %@",hkphone)
       if regexMobile.evaluate(with: phone) == true {
           return true
       }else{
           return false
       }
   }

Email验证

判断以xxxxx@xxx.xx为标准的email

 static func isEmail(email:String) -> Bool {
        let emailRegular = "^([a-zA-Z0-9]+([._\\-])*[a-zA-Z0-9]*)+@([a-zA-Z0-9])+(.([a-zA-Z])+)+$"
        let regexMobile = NSPredicate(format:"SELF MATCHES %@",emailRegular)
        if regexMobile.evaluate(with: email) == true {
            return true
        }else{
            return false
        }
    }

特定Email验证

判断特定Email后缀的验证

static func isBelowemail(email:String) -> Bool {
        let emailRegular = "^([a-zA-Z0-9]+([._\\-])*[a-zA-Z0-9]*)+@([a-zA-Z0-9])+(.([a-zA-Z])+)+$"
        let regexMobile = NSPredicate(format:"SELF MATCHES %@",emailRegular)
        if regexMobile.evaluate(with: email) == true {
            /**
             judge below email
             •    @pccw.com
             •    @hkcsl.com
             •    @pcpd.com
             •    @pccwglobal.com
             •    @viu.tv
             **/
            if let index = email.characters.index(of: "@") {
                let belowEmail = String(email.characters.suffix(from: index))
                switch belowEmail{
                case "@pccw.com":
                    return true
                case "@hkcsl.com":
                    return true
                case "@pcpd.com":
                    return true
                case "@pccwglobal.com":
                    return true
                case "@viu.tv":
                    return true
                default:
                    return false
                }
            }
            return false
        }else{
            return false
        }
    }

PWD验证

判断必须有一个大写字母,小写字母,数字8位数以上的PWD

static func isPassword(password:String) -> Bool{
        let passwordRegular = "^(?=.*[a-z])(?=.*[A-Z])(?=.*[^A-Za-z0-9])[a-zA-Z[^A-Za-z0-9]\\d]{8,}$"
        let regexMobile = NSPredicate(format:"SELF MATCHES %@",passwordRegular)
        if regexMobile.evaluate(with: password) == true {
            return true
        }else{
            return false
        }
    }
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 网络 理论模型,分为七层物理层数据链路层传输层会话层表示层应用层 实际应用,分为四层链路层网络层传输层应用层 IP...
    FlyingLittlePG阅读 4,373评论 0 0
  • 常用模块 认识模块 什么是模块 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文...
    go以恒阅读 6,258评论 0 6
  • 第5章 引用类型(返回首页) 本章内容 使用对象 创建并操作数组 理解基本的JavaScript类型 使用基本类型...
    大学一百阅读 8,906评论 0 4
  • 1/75 1认识Python语言 2/75 序言 培训最终的目标是什么? 衡量一个合格的软件工程师的标准是什么? ...
    清清子衿木子水心阅读 9,627评论 0 1
  • 作者:李平武出版社:外语教学与研究出版社版本:2002年11月第1版,2013年4月第9次印刷来源:微盘下载mob...
    马文Marvin阅读 14,849评论 5 85

友情链接更多精彩内容