-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.html
209 lines (194 loc) · 6.32 KB
/
test.html
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<!--<link rel="stylesheet" href="../css/style.css">-->
<title>任务一:表单(一)单个表单项的检验</title>
<style>
*{
margin:0;padding:0;
box-sizing: border-box;
}
:after, :before {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
body {
font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
font-size: 14px;
line-height: 1.42857143;
color: #333;
background-color: #fff;
}
.clearfix { clear: both;}
.spac4{
margin:10px;
}
.container{
width:500px;height:120px; border:1px solid #eee; position: absolute;margin:auto;top:0;left:0;right:0;bottom:0;margin:auto;
}
.form-group {
margin-right: -15px;
margin-left: -15px;
margin-bottom: 15px;
margin-top:15px;
}
.form-group:after{
display: table;
content: " ";
clear: both;
}
.text-right{
text-align: right;
}
.text-left{
text-align: left;
}
.text-center{
text-align: center;
}
.fl_left{
float: left;
}
.fl_right{
float: right;
}
.form-group .control-label {
padding-top: 7px;
margin-bottom: 0;
text-align: right;
}
#prompt{
display:block;
width:100%;
text-align: center;
margin-top:15px;
line-height:45px;
color:#AAAAAA;
}
.prompt{
width:100%;
text-align: center;
margin-top:15px;
line-height:45px;
color:#AAAAAA;
display:none;
}
.col-md-1,.col-md-2,.col-md-3,.col-md-4,.col-md-5,.col-md-6,.col-md-7,.col-md-8,.col-md-9,.col-md-10,.col-md-11,.col-md-12{
float: left;
padding-right: 15px;
padding-left: 15px;
}
.col-md-1{width:8.33333%}
.col-md-2{width:16.66667%}
.col-md-3{width:25%!important;}
.col-md-4{width:33.33333%}
.col-md-5{width:41.66667%}
.col-md-6{width:50%}
.col-md-7{width:58.33333%}
.col-md-8{width:66.66667%}
.col-md-9{width:75%}
.col-md-10{width:83.33333%}
.col-md-11{width:91.66667%}
.col-md-12{width:100%}
button, input, select, textarea {
font-family: inherit;
font-size: inherit;
line-height: inherit;
}
.form-control {
display: block;
width: 100%;
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
-o-transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
.btn {
display: inline-block;
padding: 6px 12px;
margin-bottom: 0;
font-size: 14px;
font-weight: 400;
line-height: 1.42857143;
text-align: center;
white-space: nowrap;
vertical-align: middle;
-ms-touch-action: manipulation;
touch-action: manipulation;
cursor: pointer;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
background-image: none;
border: 1px solid transparent;
border-radius: 4px;
}
.error{
border: 1px solid red;
}
.btn-primary {
color: #fff;
background-color: #337ab7;
border-color: #2e6da4;
}
</style>
</head>
<body>
<div style="font-size: 30px;font-weight: bold;margin-top:200px;text-align: center">任务一:表单(一)单个表单项的检验</div>
<div class="container">
<form action="">
<div class="form-group">
<label for="inputname" class="col-md-3 control-label">名称</label>
<div class="col-md-6">
<input type="text" class="form-control" id="inputname" placeholder="name">
</div>
<div class="col-md-3">
<button type="button" class="btn btn-primary" id="button">验证</button>
</div>
<p id="prompt">必填,长度为4~16个字符</p>
</div>
</form>
</div>
</body>
<script>
//获取的ID值
var input_name = document.getElementById("inputname");
var Btn = document.getElementById("button");
var promptLabel = document.getElementById("prompt");
//规则
var RegYchinese = /[\u4E00-\u9FA5]/g;//汉字
var regNchinese = /[^\u4E00-\u9FA5\uf900-\ufa2d]/g; //匹配非中文
var Regtrim = /^\s+|\s+$/g; // 删除字符串两侧的空白字符
Btn.addEventListener("click",function(){
var input_value = input_name.value;
//计算长度
var length = input_value.replace(Regtrim,'').replace(RegYchinese, '').length + input_value.replace(Regtrim,'').replace(regNchinese,'').length;
if(length==0){
input_name.style.border="1px solid red";
promptLabel.innerHTML= '名称不能为空';
promptLabel.style.color = "#DE0012";
}else if(length>=4&&length<=16){
input_name.style.border="1px solid green";
promptLabel.innerHTML= '名称格式正确';
promptLabel.style.color = "green";
}else{
input_name.style.border="1px solid red";
promptLabel.innerHTML= '必填,长度为4~16个字符';
promptLabel.style.color = "#DE0012";
}
})
</script>
</html>