Swift 正则验证手机号、身份证、邮箱、密码等

2017-04-28  本文已影响516人  踢足球的程序员

直接上代码:

class func checkMobile(mobileNumbel:NSString) ->Bool
    {
        /**
         * 手机号码
         * 移动:134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         * 联通:130,131,132,152,155,156,185,186
         * 电信:133,1349,153,180,189,181(增加)
         */
        let MOBIL = "^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";
        /**
         * 中国移动:China Mobile
         * 134[0-8],135,136,137,138,139,150,151,157,158,159,182,187,188
         */
        let CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[2378])\\d)\\d{7}$";
        /**
         * 中国联通:China Unicom
         * 130,131,132,152,155,156,185,186
         */
        let CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$";
        /**
         * 中国电信:China Telecom
         * 133,1349,153,180,189,181(增加)
         */
        let CT = "^1((33|53|8[019])[0-9]|349)\\d{7}$";
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@", MOBIL)
        let regextestcm = NSPredicate(format: "SELF MATCHES %@", CM)
        let regextestcu = NSPredicate(format: "SELF MATCHES %@", CU)
        let regextestct = NSPredicate(format: "SELF MATCHES %@", CT)
        if regextestmobile.evaluateWithObject(mobileNumbel)||regextestcm.evaluateWithObject(mobileNumbel)||regextestcu.evaluateWithObject(mobileNumbel)||regextestct.evaluateWithObject(mobileNumbel) {
            return true
        }
            return false
    }
    //MARK:- 正则匹配用户身份证号15或18位
    class func checkUserIdCard(idCard:NSString) ->Bool {
        let pattern = "(^[0-9]{15}$)|([0-9]{17}([0-9]|X)$)";
        let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
        let isMatch:Bool = pred.evaluateWithObject(idCard)
        return isMatch;
    }
    //MARK:- 正则匹配用户密码6-18位数字和字母组合
    class func checkPassword(password:NSString) ->Bool {
        let pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,18}"
        let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
        let isMatch:Bool = pred.evaluateWithObject(password)
        return isMatch;
    }
    //MARK:- 正则匹配URL
    class func checkURL(url:NSString) ->Bool {
        let pattern = "^[0-9A-Za-z]{1,50}"
        let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
        let isMatch:Bool = pred.evaluateWithObject(url)
        return isMatch;
    }
    //MARK:- 正则匹配用户姓名,20位的中文或英文
   class func checkUserName(userName:NSString) ->Bool {
        let pattern = "^[a-zA-Z\\u4E00-\\u9FA5]{1,20}"
        let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
        let isMatch:Bool = pred.evaluateWithObject(userName)
        return isMatch;
    }
    //MARK:- 正则匹配用户email
    class func checkEmail(email:NSString) ->Bool {
        let pattern = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"
        let pred = NSPredicate(format: "SELF MATCHES %@", pattern)
        let isMatch:Bool = pred.evaluateWithObject(email)
        return isMatch;
    }
上一篇下一篇

猜你喜欢

热点阅读