加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift:邮箱网址手机号码等正则判断

发布时间:2020-12-14 06:44:17 所属栏目:百科 来源:网络整理
导读:邮箱网址手机号码等正则判断 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 importFoundation enum Validate{ case email(_:String) phoneNum(_:String) carNu

邮箱网址手机号码等正则判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
importFoundation
enum Validate{
case email(_:String)
phoneNum(_:String)
carNum(_:String)
username(_:String)
password(_:String)
nickname(_:String)
URL(_:String)
IP(_:String)
varisRight:Bool{
varpredicateStr:String!
varcurrObject:String!
switch self{
let.email(str):
predicateStr= "^([a-z0-9_.-]+)@([da-z.-]+).([a-z.]{2,6})$"
currObject=str
let.phoneNum(str):
"^((13[0-9])|(15[^4,D])|(17[0,0-9])|(18[0,0-9]))d{8}$"
currObject=str
let.carNum(str):
"^[A-Za-z]{1}[A-Za-z_0-9]{5}$"
currObject=str
let.username(str):
"^[A-Za-z0-9]{6,20}+$"
currObject=str
let.password(str):
"^[a-zA-Z0-9]{6,20}+$"
currObject=str
let.nickname(str):
"^[u4e00-u9fa5]{4,8}$"
currObject=str
let.URL(str):
"^(https?://)?([da-z.-]+).([a-z.]{2,6})([/w.-]*)*/?$"
currObject=str
let.IP(str):
"^(?:(?: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
}
letpredicate=NSPredicate(format: "SELFMATCHES%@" ,predicateStr)
return predicate.evaluateWithObject(currObject)
}
}

使用:

5
Validate.email( "Dousnail@@153.com" ).isRight //false
Validate.URL( "https://www.baidu.com" ).isRight// true
Validate.IP( "11.11.11.11" //true

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读