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):
predicateStr = "^((13[0-9])|(15[^4,\\D]) |(17[0,0-9])|(18[0,0-9]))\\d{8}$"
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)
}
}
Swift 正则匹配手机号码、邮箱等
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...