Files
website_design/static/validate.js
T

39 lines
1.4 KiB
JavaScript

document.addEventListener('DOMContentLoaded', () => {
const signupForm = document.getElementById('signup-form');
if (signupForm) {
signupForm.addEventListener('submit', async (event) => {
event.preventDefault();
const formData = new FormData(signupForm);
const response = await fetch(signupForm.action, {
method: 'POST',
body: formData,
});
const text = await response.text();
if (text === 'Username already exists') {
alert('用户名已存在,请选择另一个用户名。');
} else if (text === 'Passwords do not match') {
alert('密码和确认密码不匹配。');
} else if (text === 'Invalid captcha') {
alert('验证码无效。');
} else {
window.location.href = '/signin';
}
});
}
const signinForm = document.querySelector('form[action="/signin"]');
if (signinForm) {
signinForm.addEventListener('submit', (event) => {
const password = document.getElementById('password').value;
const regex = /^[A-Za-z0-9]+$/;
if (!regex.test(password)) {
event.preventDefault();
alert('密码只能包含大小写字母和数字,不允许出现其他符号。');
}
});
}
});