-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask5.html
111 lines (109 loc) · 3.15 KB
/
task5.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>正则表达式之入门</title>
<style>
section {
width: 500px;
height: 150px;
padding-left: 20px;
margin: 10px auto;
border: 1px dotted #ccc;
}
input {
box-sizing: border-box;
height: 25px;
}
button {
box-sizing: border-box;
height: 25px;
vertical-align: bottom;
border: 1px solid #ccc;
background-color: transparent;
}
.intro {
width: 500px;
margin: 0 auto;
font-family: "Comic Sans MS","幼圆","黑体",sans-serif;
}
.intro h1 {
text-align: center;
}
.intro p {
text-indent: 2em;
line-height: 1.5;
font-size: 20px;
}
.intro code {
color: #111;
font: 16px sans-serif;
}
</style>
</head>
<body>
<section class="phone-num">
<h1>验证手机号码</h1>
<label>
请输入一个手机号:<input type="text" id="phone-num">
</label>
<button>提交并验证</button>
<p></p>
</section>
<section>
<h1>判断是否有相邻重复单词</h1>
<label>
请输入若干非重复单词:<input type="text" id="str">
</label>
<button>提交并验证</button>
<p></p>
</section>
<div class="intro">
<h1>正则表达式之入门</h1>
<p>验证手机:手机号码由11位数字组成,前3位组合可由运营商查询。</p>
<p>判断相邻重复单词:单词由字母(有时含有连字符)构成,重复的第一个子项,在正则表达式中,可写为<code>\1</code>,注意重复项必须完全一样,要避开特殊符号造成的单词边界形成误判,因此可以以单词两边都为空格来处理。</p>
</div>
<script>
/**
* 手机号码
* 移动:134,135,136,137,138,139,147,150,151,152,157,158,159,170,178,182,183,184,187,188
* 联通:130,131,132,145,152,155,156,1709,171,176,185,186
* 电信:133,134,153,1700,177,180,181,189
*/
//验证手机号码
var phoneBtn = document.getElementsByTagName('button')[0];
phoneBtn.onclick = function testPhoneNum() {
var phoneInput = document.getElementById('phone-num');
var phoneNum = phoneInput.value;
var phoneResult = document.getElementsByTagName('p')[0];
var phoneNumReg = /^1(3[0-9]|4[5|7]|5[0-35-9]|7[016-8]|8[0-9])\d{8}$/;
var result = phoneNumReg.test(phoneNum);
if (result) {
phoneResult.innerHTML = '这是一个手机号!';
phoneInput.style.borderColor = 'green';
} else {
phoneResult.innerHTML = '这不是一个手机号!请重新输入!';
phoneInput.value = '';
phoneInput.style.borderColor = 'red';
}
}
//判断是否有相邻重复单词
var strBtn = document.getElementsByTagName('button')[1];
strBtn.onclick = function testPhoneNum() {
var strInput = document.getElementById('str');
var strValue = strInput.value;
var strResult = document.getElementsByTagName('p')[1];
var strReg = /(\s|^)([A-Za-z]+(-[A-Za-z]+)*)\s+\2(?=\s|$)/g;
var result = strReg.test(strValue);
if (result) {
strResult.innerHTML = '有重复!请重新输入!';
strInput.value = '';
strInput.style.borderColor = 'red';
} else {
strResult.innerHTML = '没有重复!';
strInput.style.borderColor = 'green';
}
}
</script>
</body>
</html>