94 lines
2.3 KiB
HTML
94 lines
2.3 KiB
HTML
<!DOCTYPE html>
|
||
<html>
|
||
<head>
|
||
<meta charset="utf-8" />
|
||
<meta name="author" content="flykhan" />
|
||
<meta name="keywords" content="c c++ 物联网" />
|
||
<title>表单数据验证</title>
|
||
|
||
<style>
|
||
form {
|
||
width: 300px;
|
||
}
|
||
|
||
.row {
|
||
display: flex;
|
||
flex-direction: row;
|
||
flex-wrap: nowrap;
|
||
align-items: center;
|
||
}
|
||
|
||
input {
|
||
outline: none;
|
||
padding: 10px 10px;
|
||
margin: 10px;
|
||
}
|
||
|
||
.row > label {
|
||
display: inline-block;
|
||
width: 100px;
|
||
text-align: right;
|
||
}
|
||
|
||
.input_error {
|
||
color: red;
|
||
border: 1px solid red;
|
||
outline: 1px solid yellow;
|
||
}
|
||
|
||
.center {
|
||
/* 水平方向中心位置 */
|
||
justify-content: center;
|
||
}
|
||
</style>
|
||
</head>
|
||
<body>
|
||
<form onsubmit="return validForm(this)">
|
||
<div class="row">
|
||
<label>手机号</label>
|
||
<!-- onfocus 当标签获取到焦点时,调用的处理程序 -->
|
||
<input
|
||
onfocus="this.className='';"
|
||
name="phone"
|
||
placeholder="请输入手机号"
|
||
/>
|
||
</div>
|
||
<div class="row">
|
||
<label>口令</label>
|
||
<input onfocus="this.className='';" name="pwd" type="password" />
|
||
</div>
|
||
<div class="row">
|
||
<label>确认口令</label>
|
||
<input onfocus="this.className='';" name="pwd2" type="password" />
|
||
</div>
|
||
<div class="row center">
|
||
<button>登录</button>
|
||
</div>
|
||
</form>
|
||
<script>
|
||
function validForm(form) {
|
||
console.log(form.phone.value);
|
||
let hasError = false;
|
||
if (!/1[3-9]\d{9}/.test(form.phone.value)) {
|
||
form.phone.className = "input_error";
|
||
hasError = true; // 有错
|
||
}
|
||
// 验证口令:有字母数字组成的至少8位的字符串
|
||
if (!/[a-zA-Z][a-zA-Z0-9]{7,}/.test(form.pwd.value)) {
|
||
form.pwd.className = "input_error";
|
||
hasError = true; // 有错
|
||
}
|
||
|
||
// 验证两次口令是否相等
|
||
if (form.pwd.value != form.pwd2.value) {
|
||
form.pwd2.className = "input_error";
|
||
hasError = true; // 有错
|
||
}
|
||
console.log(form.pwd.value);
|
||
console.log(form.pwd2.value);
|
||
return false;
|
||
}
|
||
</script>
|
||
</body>
|
||
</html>
|