首页 热点资讯 义务教育 高等教育 出国留学 考研考公
您的当前位置:首页正文

swift手机号码验证

2024-12-17 来源:花图问答
funcvalidationPhone(phone:String)->Bool{

letphoneRegex:String="^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(14[0,0-9])|(17[0,0-9]))\\d{8}$"

letphoneTest =NSPredicate(format:"SELF MATCHES %@",phoneRegex)

returnphoneTest .evaluateWithObject(phone)

}
//根据运营商,判断手机号码,可添加最新号码集
func isTelNumber(num:String)->Bool
    {
        let mobile = "^1(7[0-9]|3[0-9]|5[0-35-9]|8[025-9])\\d{8}$"
        let  CM = "^1(34[0-8]|(3[5-9]|5[017-9]|8[278])\\d)\\d{7}$"
        let  CU = "^1(3[0-2]|5[256]|8[56])\\d{8}$"
        let  CT = "^1((33|53|8[09])[0-9]|349)\\d{7}$"
        let regextestmobile = NSPredicate(format: "SELF MATCHES %@",mobile)
        let regextestcm = NSPredicate(format: "SELF MATCHES %@",CM )
        let regextestcu = NSPredicate(format: "SELF MATCHES %@" ,CU)
        let regextestct = NSPredicate(format: "SELF MATCHES %@" ,CT)
        if ((regextestmobile.evaluate(with: num) == true)
            || (regextestcm.evaluate(with: num)  == true)
            || (regextestct.evaluate(with: num) == true)
            || (regextestcu.evaluate(with: num) == true))
        {
            return true
        }
        else
        {
            return false
        }
    }

显示全文