内容字号:默认大号超大号

段落设置:段首缩进取消段首缩进

字体设置:切换到微软雅黑切换到宋体

swift正则表达式之NSPredicate谓词版

2017-08-12 19:18 出处:清屏网 人气: 评论(0

方法如下:

enum verifyType{
    
    case Email
    case PhoneNumber
    case IdentityCard
    
}

extension String{
    
    //正则验证
    func verifyText(type: verifyType) -> Bool{
        
        if type == .PhoneNumber{
            /*
             * 所有手机号
             * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
             */
            let MOBILE = "^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[0678])\\d{8}$"
            
            /*
             * 中国移动 China Mobile
             * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
             */
            let CM = "(^1(3[4-9]|5[0-27-9]|8[2-478]|47|78)\\d{8}$)|(^1705\\d{7}$)"
            
            /*
             * 中国联通 China Unicom
             * 130,131,132,155,156,185,186,145,176,1709
             */
            let CU = "(^1(3[0-2]|5[56]|8[56]|45|76)\\d{8}$)|(^1709\\d{7}$)"
            
            /*
             *中国电信 China Telecom
             *133,153,180,181,189,177,1700
             */
            let CT = "(^1(33|53|8[019]|77)\\d{8}$)|(^1700\\d{7}$)"
            
            //谓词
            let RegexTestMobile = NSPredicate.init(format: "SELF MATCHES %@", MOBILE)
            let RegexTestCM = NSPredicate.init(format: "SELF MATCHES %@", CM)
            let RegexTestCU = NSPredicate.init(format: "SELF MATCHES %@", CU)
            let RegexTestCT = NSPredicate.init(format: "SELF MATCHES %@", CT)
            
            if RegexTestMobile.evaluate(with: self) || RegexTestCM.evaluate(with: self) || RegexTestCU.evaluate(with: self) || RegexTestCT.evaluate(with: self) {
                return true
            }else{
                return false
            }
            
        }else{
            return false
        }
    }
}

暂时只写了手机号的,以后会慢慢增加其他的正则表达式。

这种验证 let RegexTestMobile = NSPredicate.init(format: "SELF MATCHES %@", MOBILE) 就这么一句话,代码简洁了不少。于是我就去了解了一下NSPredicate(谓词)。

大致可以替代NSRegularExpression,验证的代码很少,直接贴代码了。

主页:

class ViewController: UIViewController,UITextFieldDelegate {

    var phoneNumberText = UITextField()
    var randomButton = UIButton()
    var verifyButton = UIButton()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        phoneNumberText.frame = CGRect.init(x: 60, y: 100, width: SCREEN_WIDTH - 120, height: 50)
        phoneNumberText.borderStyle = .line
        phoneNumberText.delegate = self
        self.view.addSubview(phoneNumberText)
        
        randomButton.frame = CGRect.init(x: 60, y: 180, width: 120, height: 44)
        randomButton.backgroundColor = UIColor.orange
        randomButton.setTitle("产生随机手机号", for: .normal)
        randomButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        randomButton.addTarget(self, action: #selector(getRandomTel), for: .touchUpInside)
        self.view.addSubview(randomButton)
        
        verifyButton.frame = CGRect.init(x: SCREEN_WIDTH - 180, y: 180, width: 120, height: 44)
        verifyButton.backgroundColor = UIColor.red
        verifyButton.setTitle("验证手机号", for: .normal)
        verifyButton.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        verifyButton.addTarget(self, action: #selector(verify), for: .touchUpInside)
        self.view.addSubview(verifyButton)
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func getRandomTel(){
        
        if phoneNumberText.resignFirstResponder(){
            phoneNumberText.resignFirstResponder()
        }
        
        phoneNumberText.text = creatRandomTel()
        
    }
    
    //获取随机手机号
    func creatRandomTel() -> String{
        
        var phoneNumber = "1"
        for _ in 0..<10{
            phoneNumber = phoneNumber + "\(arc4random_uniform(9))"
        }
        
        return phoneNumber
        
    }

    //验证手机号
    func verify(){
        
       print(phoneNumberText.text?.verifyText(type: .PhoneNumber) ?? "")
        
    }
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        let result = textField.text?.appending(string)
        guard (result?.characters.count)! > 11 else {
            return true
        }
        return false
    }
}
分享给小伙伴们:
本文标签: NSPredicate正则表达式swift

相关文章

发表评论愿您的每句评论,都能给大家的生活添色彩,带来共鸣,带来思索,带来快乐。

CopyRight © 2015-2016 QingPingShan.com , All Rights Reserved.

清屏网 版权所有 豫ICP备15026204号