HTML/Html-practice/jQuery/01-jQuery-表单验证.html

82 lines
2.1 KiB
HTML
Raw Normal View History

2023-05-01 19:37:40 +08:00
<!DOCTYPE html>
<html lang="en">
2023-05-01 22:22:17 +08:00
2023-05-01 19:37:40 +08:00
<head>
2023-05-01 22:22:17 +08:00
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="./js/FormValidation.js"></script>
<link href="./css/FormValidation.css" rel="stylesheet" type="text/css" />
<title>jQuery-表单验证</title>
2023-05-01 19:37:40 +08:00
</head>
2023-05-01 22:22:17 +08:00
2023-05-01 19:37:40 +08:00
<body>
2023-05-01 22:22:17 +08:00
<div class="main">
<form action="form" onsubmit="return func();">
<div class="container">
<div class="card">
<a class="singup">jQuery-表单验证</a>
<div class="inputBox">
<input type="text" required="required" class="Username">
<span class="info1">Username</span>
</div>
<div class="inputBox1">
<input type="text" required="required" class="Email">
<span class="info2">Email</span>
</div>
<div class="inputBox">
<input type="password" required="required" class="Password">
<span class="info3">Password</span>
</div>
<button class="enter" type="submit">Enter</button>
2023-05-01 19:37:40 +08:00
</div>
2023-05-01 22:22:17 +08:00
</div>
</div>
</form>
2023-05-01 19:37:40 +08:00
</body>
<script>
2023-05-01 22:22:17 +08:00
$(function () {
2023-05-01 22:47:22 +08:00
// 给username 输入框绑定焦点失去事件
2023-05-01 19:37:40 +08:00
$('.Username').blur(function () {
2023-05-01 22:47:22 +08:00
$('.Username').tooltip('userName'); // 用户名验证函数
2023-05-01 19:37:40 +08:00
})
2023-05-01 22:47:22 +08:00
// Email 输入框绑定焦点失去事件
2023-05-01 19:37:40 +08:00
$('.Email').blur(function () {
2023-05-01 22:47:22 +08:00
$('.Email').tooltip('email'); // 邮箱验证函数
2023-05-01 19:37:40 +08:00
})
2023-05-01 22:47:22 +08:00
// Password 输入框绑定焦点失去事件
2023-05-01 19:37:40 +08:00
$('.Password').blur(function () {
2023-05-01 22:47:22 +08:00
$('.Password').tooltip('password'); // 密码验证函数
2023-05-01 19:37:40 +08:00
})
2023-05-01 22:10:16 +08:00
2023-05-01 22:22:17 +08:00
2023-05-01 19:37:40 +08:00
})
2023-05-01 22:22:17 +08:00
2023-05-01 22:47:22 +08:00
// 表单验证,全为true 则提交,反之不提交
2023-05-01 22:22:17 +08:00
function func() {
var flag1 = $('.Username').tooltip('userName');
var flag2 = $('.Email').tooltip('email');
var flag3 = $('.Password').tooltip('password');
if (flag1 && flag2 && flag3) {
2023-05-01 22:10:16 +08:00
return true;
2023-05-01 22:22:17 +08:00
} else {
alert("请填写正确的用户名密码或邮箱!!!");
return false;
}
2023-05-01 22:10:16 +08:00
}
2023-05-01 19:37:40 +08:00
</script>
</html>