5
5
< title > Strategy</ title >
6
6
</ head >
7
7
< body >
8
- < script src ="http://code.jquery.com/jquery-1.7 .2.min.js "> </ script >
8
+ < script src ="http://code.jquery.com/jquery-1.8 .2.min.js "> </ script >
9
9
< script >
10
- var validator = {
11
- types : { } , //所有的驗證規則皆會存放於此,稍後會個別定義
12
- messages : [ ] , //個別驗證類型的錯誤訊息
13
- config : { } , //使用者資料各欄位需要被驗證的類型
14
- validate : function ( data ) { // 使用者資料 - 欄位:值
15
- var i ,
16
- msg ,
17
- type ,
18
- checker ,
19
- result_ok ;
20
- this . messages = [ ] ; //清空所有的錯誤信息
21
-
22
- for ( i in data ) {
23
- if ( data . hasOwnProperty ( i ) ) { //判斷使用者欄位是否需要被驗證
24
- type = this . config [ i ] ; //如果需要被驗證,則取出相對應的驗證規則
25
- checker = this . types [ type ] ;
26
- if ( ! type ) {
27
- continue ; //如果驗證規則不存在,則不處理
28
- }
29
- if ( ! checker ) { //如果驗證規則類不存在,拋出異常
30
- throw {
31
- name : "ValidationError" ,
32
- message : "No handler to validate type " + type
33
- } ;
34
- }
35
- result_ok = checker . validate ( data [ i ] ) ; //驗證
36
- if ( ! result_ok ) { //取得錯誤訊息
37
- msg = "Invalid value for *" + i + "*, " + checker . instructions ;
38
- this . messages . push ( msg ) ;
39
- }
10
+ var validator = {
11
+ types : { } , //所有的驗證規則皆會存放於此,稍後會個別定義
12
+ messages : [ ] , //個別驗證類型的錯誤訊息
13
+ config : { } , //使用者資料各欄位需要被驗證的類型
14
+ validate : function ( data ) { // 使用者資料 - 欄位:值
15
+ var i ,
16
+ msg ,
17
+ type ,
18
+ checker ,
19
+ result_ok ;
20
+ this . messages = [ ] ; //清空所有的錯誤信息
21
+
22
+ for ( i in data ) {
23
+ if ( data . hasOwnProperty ( i ) ) { //判斷使用者欄位是否需要被驗證
24
+ type = this . config [ i ] ; //如果需要被驗證,則取出相對應的驗證規則
25
+ checker = this . types [ type ] ;
26
+ if ( ! type ) {
27
+ continue ; //如果驗證規則不存在,則不處理
28
+ }
29
+ if ( ! checker ) { //如果驗證規則類不存在,拋出異常
30
+ throw {
31
+ name : "ValidationError" ,
32
+ message : "No handler to validate type " + type
33
+ } ;
34
+ }
35
+ result_ok = checker . validate ( data [ i ] ) ; //驗證
36
+ if ( ! result_ok ) { //取得錯誤訊息
37
+ msg = "Invalid value for *" + i + "*, " + checker . instructions ;
38
+ this . messages . push ( msg ) ;
40
39
}
41
40
}
42
- return this . hasErrors ( ) ;
43
- } ,
44
- //helper
45
- hasErrors :function ( ) {
46
- return this . messages . length !== 0 ;
47
41
}
48
- } ;
49
-
50
- //個別定義驗證規則
51
- //欄位值不可為空
52
- validator . types . isNonEmpty = {
53
- validate :function ( value ) {
54
- return value !== "" ;
55
- } ,
56
- instructions : "the value cannot be empty"
57
- } ;
58
-
59
- //欄位值只能為數字
60
- validator . types . isNumber = {
61
- validate :function ( value ) {
62
- return ! isNaN ( value ) ;
63
- } ,
64
- instructions : "the value can only be a valid number, e.g. 1, 3.14 or 2010"
65
- } ;
66
-
67
- //欄位值是否為英數組合
68
- validator . types . isAlphaNum = {
69
- validate :function ( value ) {
70
- return ! / [ ^ a - z 0 - 9 ] / i. test ( value ) ;
71
- } ,
72
- instructions : "the value can only contain characters and numbers, no special symbols"
73
- } ;
74
-
75
- //欄位值是否為Email
76
- validator . types . isEmail = {
77
- validate : function ( value ) {
78
- var re = / ^ ( [ \w - ] + (?: \. [ \w - ] + ) * ) @ ( (?: [ \w - ] + \. ) * \w [ \w - ] { 0 , 66 } ) \. ( [ a - z ] { 2 , 6 } (?: \. [ a - z ] { 2 } ) ? ) $ / i;
79
- return re . test ( value ) ;
80
- } ,
81
- instructions : 'use valid email format, e.g. @'
82
- } ;
83
-
84
- //欄位值有最小長度限制,字串大小是否在最小範圍內,設定為3個characters以上(minSize)
85
- validator . types . minSize = {
86
- validate : function ( value ) {
87
- return value . length >= 3 ;
88
- } ,
89
- instructions : 'min size is 3 characters'
90
- } ;
91
-
92
- //欄位值有最大長度限制,字串大小是否在最大範圍內,設定為10個characters以內(maxSize)
93
- validator . types . maxSize = {
94
- validate : function ( value ) {
95
- return value . length <= 10 ;
96
- } ,
97
- instructions : 'max size is 10 characters'
98
- } ;
99
-
100
- //測試資料
101
- var data = {
102
- first_name : 'Super' ,
103
- last_name : 'Man' ,
104
- age : 'unknown' ,
105
- username : 'o_O' ,
106
-
107
- confirm_email : 'invalid_email_sample' ,
108
- password : '12'
109
- } ;
110
-
111
- //定義測試資料每個欄位需要被驗證的類型
112
- validator . config = {
113
- first_name : 'isNonEmpty' ,
114
- last_name : 'maxSize' ,
115
- age : 'isNumber' ,
116
- username : 'isAlphaNum' ,
117
- email : 'isEmail' ,
118
- confirm_email : 'isEmail' ,
119
- password : 'minSize'
120
- } ;
121
-
122
- //若有錯誤,則console出錯誤訊息
123
- validator . validate ( data ) ;
124
- if ( validator . hasErrors ( ) ) {
125
- console . log ( validator . messages . join ( "\n" ) ) ;
42
+ return this . hasErrors ( ) ;
43
+ } ,
44
+ //helper
45
+ hasErrors :function ( ) {
46
+ return this . messages . length !== 0 ;
126
47
}
48
+ } ;
49
+
50
+ //個別定義驗證規則
51
+ //欄位值不可為空
52
+ validator . types . isNonEmpty = {
53
+ validate :function ( value ) {
54
+ return value !== "" ;
55
+ } ,
56
+ instructions : "the value cannot be empty"
57
+ } ;
58
+
59
+ //欄位值只能為數字
60
+ validator . types . isNumber = {
61
+ validate :function ( value ) {
62
+ return ! isNaN ( value ) ;
63
+ } ,
64
+ instructions : "the value can only be a valid number, e.g. 1, 3.14 or 2010"
65
+ } ;
66
+
67
+ //欄位值是否為英數組合
68
+ validator . types . isAlphaNum = {
69
+ validate :function ( value ) {
70
+ return ! / [ ^ a - z 0 - 9 ] / i. test ( value ) ;
71
+ } ,
72
+ instructions : "the value can only contain characters and numbers, no special symbols"
73
+ } ;
74
+
75
+ //欄位值是否為Email
76
+ validator . types . isEmail = {
77
+ validate : function ( value ) {
78
+ var re = / ^ ( [ \w - ] + (?: \. [ \w - ] + ) * ) @ ( (?: [ \w - ] + \. ) * \w [ \w - ] { 0 , 66 } ) \. ( [ a - z ] { 2 , 6 } (?: \. [ a - z ] { 2 } ) ? ) $ / i;
79
+ return re . test ( value ) ;
80
+ } ,
81
+ instructions : 'use valid email format, e.g. @'
82
+ } ;
83
+
84
+ //欄位值有最小長度限制,字串大小是否在最小範圍內,設定為3個characters以上(minSize)
85
+ validator . types . minSize = {
86
+ validate : function ( value ) {
87
+ return value . length >= 3 ;
88
+ } ,
89
+ instructions : 'min size is 3 characters'
90
+ } ;
91
+
92
+ //欄位值有最大長度限制,字串大小是否在最大範圍內,設定為10個characters以內(maxSize)
93
+ validator . types . maxSize = {
94
+ validate : function ( value ) {
95
+ return value . length <= 10 ;
96
+ } ,
97
+ instructions : 'max size is 10 characters'
98
+ } ;
99
+
100
+ //測試資料
101
+ var data = {
102
+ first_name : 'Super' ,
103
+ last_name : 'Man' ,
104
+ age : 'unknown' ,
105
+ username : 'o_O' ,
106
+
107
+ confirm_email : 'invalid_email_sample' ,
108
+ password : '12'
109
+ } ;
110
+
111
+ //定義測試資料每個欄位需要被驗證的類型
112
+ validator . config = {
113
+ first_name : 'isNonEmpty' ,
114
+ last_name : 'maxSize' ,
115
+ age : 'isNumber' ,
116
+ username : 'isAlphaNum' ,
117
+ email : 'isEmail' ,
118
+ confirm_email : 'isEmail' ,
119
+ password : 'minSize'
120
+ } ;
121
+
122
+ //若有錯誤,則console出錯誤訊息
123
+ validator . validate ( data ) ;
124
+ if ( validator . hasErrors ( ) ) {
125
+ console . log ( validator . messages . join ( "\n" ) ) ;
126
+ }
127
127
</ script >
128
128
</ body >
129
129
</ html >
0 commit comments