驗證操作類formValidatorClass.js
1
/**
2
* @author ming
3
*/
4
$(document).ready(function(){
5
6
/* 設置默認屬性 */
7
$.validator.setDefaults({
8
submitHandler: function(form) {
9
form.submit();
10
}
11
});
12
13
// 字符驗證
14
jQuery.validator.addMethod("stringCheck", function(value, element) {
15
return this.optional(element) || /^[\u0391-\uFFE5\w]+$/.test(value);
16
}, "只能包括中文字、英文字母、數字和下劃線");
17
18
// 中文字兩個字節
19
jQuery.validator.addMethod("byteRangeLength", function(value, element, param) {
20
var length = value.length;
21
for(var i = 0; i < value.length; i++){
22
if(value.charCodeAt(i) > 127){
23
length++;
24
}
25
}
26
return this.optional(element) || ( length >= param[0] && length <= param[1] );
27
}, "請確保輸入的值在3-15個字節之間(一個中文字算2個字節)");
28
29
// 身份證號碼驗證
30
jQuery.validator.addMethod("isIdCardNo", function(value, element) {
31
return this.optional(element) || isIdCardNo(value);
32
}, "請正確輸入您的身份證號碼");
33
34
// 手機號碼驗證
35
jQuery.validator.addMethod("isMobile", function(value, element) {
36
var length = value.length;
37
var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
38
return this.optional(element) || (length == 11 && mobile.test(value));
39
}, "請正確填寫您的手機號碼");
40
41
// 電話號碼驗證
42
jQuery.validator.addMethod("isTel", function(value, element) {
43
var tel = /^\d{3,4}-?\d{7,9}$/; //電話號碼格式010-12345678
44
return this.optional(element) || (tel.test(value));
45
}, "請正確填寫您的電話號碼");
46
47
// 聯系電話(手機/電話皆可)驗證
48
jQuery.validator.addMethod("isPhone", function(value,element) {
49
var length = value.length;
50
var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
51
var tel = /^\d{3,4}-?\d{7,9}$/;
52
return this.optional(element) || (tel.test(value) || mobile.test(value));
53
54
}, "請正確填寫您的聯系電話");
55
56
// 郵政編碼驗證
57
jQuery.validator.addMethod("isZipCode", function(value, element) {
58
var tel = /^[0-9]{6}$/;
59
return this.optional(element) || (tel.test(value));
60
}, "請正確填寫您的郵政編碼");
61
62
//開始驗證
63
$('#submitForm').validate({
64
/* 設置驗證規則 */
65
rules: {
66
username: {
67
required:true,
68
stringCheck:true,
69
byteRangeLength:[3,15]
70
},
71
email:{
72
required:true,
73
email:true
74
},
75
phone:{
76
required:true,
77
isPhone:true
78
},
79
address:{
80
required:true,
81
stringCheck:true,
82
byteRangeLength:[3,100]
83
}
84
},
85
86
/* 設置錯誤信息 */
87
messages: {
88
username: {
89
required: "請填寫用戶名",
90
stringCheck: "用戶名只能包括中文字、英文字母、數字和下劃線",
91
byteRangeLength: "用戶名必須在3-15個字符之間(一個中文字算2個字符)"
92
},
93
email:{
94
required: "請輸入一個Email地址",
95
email: "請輸入一個有效的Email地址"
96
},
97
phone:{
98
required: "請輸入您的聯系電話",
99
isPhone: "請輸入一個有效的聯系電話"
100
},
101
address:{
102
required: "請輸入您的聯系地址",
103
stringCheck: "請正確輸入您的聯系地址",
104
byteRangeLength: "請詳實您的聯系地址以便于我們聯系您"
105
}
106
},
107
108
/* 設置驗證觸發事件 */
109
focusInvalid: false,
110
onkeyup: false,
111
112
/* 設置錯誤信息提示DOM */
113
errorPlacement: function(error, element) {
114
error.appendTo( element.parent());
115
},
116
117
});
118
119
});

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

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

測試頁index.html
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
2
"http://www.w3.org/TR/html4/loose.dtd">
3
<html xmlns="http://www.w3.org/1999/xhtml">
4
<head>
5
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
6
<title>jQuery驗證</title>
7
<script src="lib/jquery/jquery-1.3.2.min.js" ></script>
8
<script type="text/javascript" src="lib/jquery/jquery.validate.js" mce_src="lib/jquery/jquery.validate.js"></script>
9
<script type="text/javascript" src="lib/jquery/messages_cn.js"></script>
10
<script type="text/javascript" src="lib/jquery/formValidatorClass.js"></script>
11
<style type="text/css">
12
13
* {
14
font-family: Verdana;
15
font-size: 96%;
16
}
17
label {
18
width: 10em;
19
float: left;
20
}
21
label.error {
22
float: none;
23
color: red;
24
padding-left: .5em;
25
vertical-align: top;
26
}
27
p {
28
clear: both;
29
}
30
.submit {
31
margin-left: 12em;
32
}
33
em {
34
font-weight: bold;
35
padding-right: 1em;
36
vertical-align: top;
37
}
38
39
</style>
40
</head>
41
<body>
42
<form class="submitForm" id="submitForm" method="get" action="">
43
<fieldset>
44
<legend>表單驗證</legend>
45
<p>
46
<label for="username">用戶名</label>
47
<em>*</em><input id="userName" name="username" size="25" />
48
</p>
49
<p>
50
<label for="email">E-Mail</label>
51
<em>*</em><input id="email" name="email" size="25" />
52
</p>
53
<p>
54
<label for="phone">聯系電話</label>
55
<em>*</em><input id="phone" name="phone" size="25" value="" />
56
</p>
57
<p>
58
<label for="address">地址</label>
59
<em>*</em><input id="address" name="address" size="22">
60
</p>
61
<input class="submit" type="submit" value="提交"/>
62
</p>
63
</fieldset>
64
</form>
65
</body>
66
</html>

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
