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 lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>发送验证码</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<div style="max-width: 500px; margin: 0 auto; padding: 20px;">
<h2>发送验证码</h2>
<form id="verification-form">
<div style="margin-bottom: 15px;">
<label for="email">邮箱地址:</label>
<input type="email" id="email" name="email" placeholder="请输入邮箱地址" required style="width: 100%; padding: 8px; margin-top: 5px;">
</div>
<button type="submit" id="send-btn" style="padding: 10px 20px; background-color: #4CAF50; color: white; border: none; cursor: pointer;">发送验证码</button>
</form>
<div id="message" style="margin-top: 20px; padding: 10px; border-radius: 5px; display: none;"></div>
</div>
<script>
$(document).ready(function() {
$('#verification-form').on('submit', function(e) {
e.preventDefault();
// 获取邮箱地址
const email = $('#email').val();
// 验证邮箱格式
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
$('#message').css('background-color', '#f44336').text('请输入有效的邮箱地址!');
$('#message').show();
return;
}
// 禁用按钮,防止重复点击
$('#send-btn').prop('disabled', true).text('发送中...');
// 发送AJAX请求
$.ajax({
url: '/mail/send',
type: 'GET',
data: { email: email },
success: function(response) {
if (response.success) {
$('#message').css('background-color', '#4CAF50').text('验证码已发送,请查收!');
} else {
$('#message').css('background-color', '#f44336').text(response.message || '发送失败,请重试!');
}
$('#message').show();
},
error: function() {
$('#message').css('background-color', '#f44336').text('网络错误,请重试!');
$('#message').show();
},
complete: function() {
// 恢复按钮状态
$('#send-btn').prop('disabled', false).text('发送验证码');
}
});
});
// 5秒后自动隐藏消息
const observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.target.id === 'message') {
setTimeout(function() {
$('#message').hide();
}, 5000);
}
});
});
observer.observe(document.getElementById('message'), {
attributes: true,
childList: true,
characterData: true
});
});
</script>
</body>
</html>
|