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验证

判断以[email protected]为标准的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
        }
    }

你可能感兴趣的:(Swift 常用NSPredicate)