swift -> 电话号码,邮箱地址 , 等, 正则判断
import Foundation
enum Validate { case email(_: String) case phoneNum(_: String) case carNum(_: String) case username(_: String) case password(_: String) case nickname(_: String) case URL(_: String) case IP(_: String) var isRight: Bool { var predicateStr:String! var currObject:String! switch self { case let .email(str): predicateStr = "^([a-z0-9_\\.-]+)@([\\da-z\\.-]+)\\.([a-z\\.]{2,6})$" currObject = str case let .phoneNum(str): //意思 第一位 1 , 第二位 [2-9] 2到9都可以, d{9} 数字9位 predicateStr = "^((1[2-9]))\\d{9}$" currObject = str case let .carNum(str): predicateStr = "^[A-Za-z]{1}[A-Za-z_0-9]{5}$" currObject = str case let .username(str): predicateStr = "^[A-Za-z0-9]{6,20}+$" currObject = str case let .password(str): predicateStr = "^[a-zA-Z0-9]{6,20}+$" currObject = str case let .nickname(str): predicateStr = "^[\\u4e00-\\u9fa5]{4,8}$" currObject = str case let .URL(str): predicateStr = "^(https?:\\/\\/)?([\\da-z\\.-]+)\\.([a-z\\.]{2,6})([\\/\\w \\.-]*)*\\/?$" currObject = str case let .IP(str): 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]?)$" currObject = str } let predicate = NSPredicate(format: "SELF MATCHES %@" ,predicateStr) return predicate.evaluate(with: currObject) } }
使用
Validate.email("Dousnail@@153.com").isRight //false Validate.URL("https://www.baidu.com").isRight //true Validate.IP("11.11.11.11").isRight //true
相关推荐
梦的天空 2020-08-25
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26