在項目中,咱們在驗證用戶的輸入,最簡單的方法就是使用正則表達式了,ios系統也提供很方便的方法,讓咱們能夠輕鬆的來驗證用戶名,密碼,身份證好 ,手機號等; ios
下面附上我在項目中用到的正則表達式的驗證方法, git
因爲比較經常使用,你們最好封裝成+靜態的方法,便於使用: 正則表達式
我把經常使用的方法,使用靜態方法封裝到一個 Utils類裏面,直接使用類名調用便可: lua
頭文件: url
使用方法示例: spa
擴展NSString,添加如下方法: .net
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
-
(
BOOL
)
hyb
_
isValidPersonID
{
return
[
NSString
hyb_isValidPersonID
:self
]
;
}
+
(
BOOL
)
hyb_isValidPersonID
:
(
NSString
*
)
personId
{
// 判斷位數
if
(
personId
.
length
!=
15
&&
personId
.
length
!=
18
)
{
return
NO
;
}
NSString
*carid
=
personId
;
long
lSumQT
=
0
;
// 加權因子
int
R
[
]
=
{
7
,
9
,
10
,
5
,
8
,
4
,
2
,
1
,
6
,
3
,
7
,
9
,
10
,
5
,
8
,
4
,
2
}
;
// 校驗碼
unsigned
char
checkers
[
11
]
=
{
'1'
,
'0'
,
'X'
,
'9'
,
'8'
,
'7'
,
'6'
,
'5'
,
'4'
,
'3'
,
'2'
}
;
// 將15位身份證號轉換成18位
NSMutableString
*str
=
[
NSMutableString
stringWithString
:personId
]
;
if
(
personId
.
length
==
15
)
{
[
str
insertString
:
@"19"
atIndex
:
6
]
;
long
p
=
0
;
const
char
*personId
=
[
str
UTF8String
]
;
for
(
int
i
=
0
;
i
<=
16
;
i
++
)
{
p
+=
(
personId
[
i
]
-
48
)
*
R
[
i
]
;
}
int
o
=
p
%
11
;
NSString
*string_content
=
[
NSString
stringWithFormat
:
@"%c"
,
checkers
[
o
]
]
;
[
str
insertString
:string_content
atIndex
:
[
str
length
]
]
;
carid
=
str
;
}
// 判斷地區碼
NSString
*
sProvince
=
[
carid
substringToIndex
:
2
]
;
if
(
!
[
self
_areaCode
:sProvince
]
)
{
return
NO
;
}
// 判斷年月日是否有效
// 年份
int
strYear
=
[
[
self
_substringWithString
:carid
begin
:
6
end
:
4
]
intValue
]
;
// 月份
int
strMonth
=
[
[
self
_substringWithString
:carid
begin
:
10
end
:
2
]
intValue
]
;
// 日
int
strDay
=
[
[
self
_substringWithString
:carid
begin
:
12
end
:
2
]
intValue
]
;
NSTimeZone
*localZone
=
[
NSTimeZone
localTimeZone
]
;
NSDateFormatter
*dateFormatter
=
[
[
NSDateFormatter
alloc
]
init
]
;
[
dateFormatter
setDateStyle
:NSDateFormatterMediumStyle
]
;
[
dateFormatter
setTimeStyle
:NSDateFormatterNoStyle
]
;
[
dateFormatter
setTimeZone
:localZone
]
;
[
dateFormatter
setDateFormat
:
@"yyyy-MM-dd HH:mm:ss"
]
;
NSDate
*date
=
[
dateFormatter
dateFrostr
:
[
NSString
stringWithFormat
:
@"%d-%d-%d 12:01:01"
,
strYear
,
strMonth
,
strDay
]
]
;
if
(
date
==
nil
)
{
return
NO
;
}
const
char
*pid
=
[
carid
UTF8String
]
;
// 檢驗長度
if
(
18
!=
strlen
(
pid
)
)
return
NO
;
// 校驗數字
for
(
int
i
=
0
;
i
<
18
;
i
++
)
{
if
(
!
isdigit
(
pid
[
i
]
)
&&
!
(
(
'X'
==
pid
[
i
]
||
'x'
==
pid
[
i
]
)
&&
17
==
i
)
)
{
return
NO
;
}
}
// 驗證最末的校驗碼
for
(
int
i
=
0
;
i
<=
16
;
i
++
)
{
lSumQT
+=
(
pid
[
i
]
-
48
)
*
R
[
i
]
;
}
if
(
checkers
[
lSumQT
%
11
]
!=
pid
[
17
]
)
{
return
NO
;
}
return
YES
;
}
#pragma mark - Private
+
(
NSString
*
)
_substringWithString
:
(
NSString
*
)
str
begin
:
(
NSInteger
)
begin
end
:
(
NSInteger
)
end
{
return
[
str
substringWithRange
:NSMakeRange
(
begin
,
end
)
]
;
}
|
這段代碼是嚴格地驗證15位和18位的身份證的,而且x/X是不一樣的,x是不能經過的。 orm