在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ 1 import Foundation 2 3 enum Validate { 4 case email(_: String) // 邮箱 5 case phoneNum(_: String) // 手机 6 case carNum(_: String) // 车牌号 7 case username(_: String) // 登录名 8 case password(_: String) // 密码 9 case nickname(_: String) // 昵称 10 case URL(_: String) // 网址 11 case IP(_: String) // ip 12 case idCard(_: String) // 身份证号 13 case numbers(_: String) // 只包含数字 14 case lettersAndNumbers(_: String) // 只包含字母和数字 15 case chinese(_: String) // 中文 16 17 var isRight: Bool { 18 var predicateStr:String! 19 var currObject:String! 20 switch self { 21 case let .email(str): 22 predicateStr = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$" 23 currObject = str 24 case let .phoneNum(str): 25 predicateStr = "^1(3[4-9]|4[7]|5[0-27-9]|7[08]|8[2-478])\\d{8}$" currObject = str 26 case let .carNum(str): 27 predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$" 28 currObject = str 29 case let .username(str): 30 predicateStr = "^[A-Za-z0-9]{6,20}+$" 31 currObject = str 32 case let .password(str): 33 predicateStr = "^[a-zA-Z0-9]{6,12}+$" 34 currObject = str 35 case let .nickname(str): 36 predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$" 37 currObject = str 38 case let .URL(str): 39 predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$" 40 currObject = str 41 case let .IP(str): 42 predicateStr = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" 43 currObject = str 44 case let .idCard(str): 45 predicateStr = "(^[1-9]\\d{5}(18|19|([23]\\d))\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$)|(^[1-9]\\d{5}\\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\\d{2}$)" 46 currObject = str 47 case let .numbers(str): 48 predicateStr = "^[0-9]+$" 49 currObject = str 50 case let .lettersAndNumbers(str): 51 predicateStr = "^[a-zA-Z0-9]{1,30}+$" 52 currObject = str 53 case let .chinese(str): 54 predicateStr = "^[\\u4e00-\\u9fa5]{0,100}$" 55 currObject = str 56 } 57 58 let predicate = NSPredicate(format: "SELF MATCHES %@" ,predicateStr) 59 return predicate.evaluate(with: currObject) 60 } 61 }
|
请发表评论