-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask29.html
More file actions
84 lines (84 loc) · 1.75 KB
/
task29.html
File metadata and controls
84 lines (84 loc) · 1.75 KB
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
<!DOCTYPE html>
<html>
<head>
<title>Task 29</title>
<meta charset="utf-8">
<style type="text/css">
body {
padding-top: 20%;
width: 50%;
height: 100%;
margin: 0 auto;
}
span {
padding-left: 10%;
display: inline-block;
width: 10%;
font-size: 24px;
color: #696969;
}
input {
width: 50%;
height: 50px;
border:2px solid #00FFFF;
border-radius: 25px;
outline: none;
font-size: 18px;
}
button {
width: 10%;
height: 50px;
border:2px solid #98Fb98;
border-radius: 25px;
background-color: #98Fb98;
font-size: 18px;
color: #708090;
outline: none;
}
div {
width: 50%;
margin: 0 auto;
font-size: 18px;
color: #696969;
margin-top: 1%;
}
</style>
</head>
<body>
<span>名称</span>
<input type="text" id="text">
<button id="btn" onclick="test()">验证</button>
<div id="remind">必填,长度为4~16个字符</div>
<script type="text/javascript">
var text = document.getElementById('text');
var button = document.getElementById('btn');
var remind = document.getElementById('remind');
function test () {
var charLength = getLength();
if (charLength >=4 && charLength <= 16) {
remind.innerHTML = "格式正确";
remind.style.color = "#00FF00";
} else if(charLength < 4) {
remind.innerHTML = "字符数小于4!";
remind.style.color = "#FF6347";
} else {
remind.innerHTML = "字符数大于16!";
remind.style.color = "#FF6347";
}
}
function getLength() {
var length = 0;
var testText = text.value;
for(var i = 0 ; i < testText.length ; i++) {
charCode = testText.charCodeAt(i);
if (charCode >=0 &&charCode <= 128) {
length += 1;
} else {
length += 2;
}
}
return length;
}
</script>
</body>
</html>