提问人:Janosz Крајишник 提问时间:4/28/2022 最后编辑:XunJanosz Крајишник 更新时间:4/28/2022 访问量:251
Javascript 表单验证适用于一种表单,但不适用于另一种表单
Javascript form validation works on one form but doesn't work on another
问:
我为我的注册表单进行了客户端表单验证,一切正常。但是,在我的登录表单上,验证无法正常工作。在登录表单上,它只显示错误状态消息,只有当我输入正确的值时,它才会显示成功消息和绿色边框。我遇到的另一个问题是错误消息的文本不是红色或绿色,只有输入的边框会改变颜色。两者的 Javascript 代码完全相同,只是 html 不同。
HTML 注册表单
<form id="signup-form" class="form" method="POST" action="./includes/signup.inc.php">
<div class="mb-3 input-control">
<label for="full-name">Full name\User name</label><br>
<p>*You can only have on user name per e-mail account</p>
<input type="text" class="form-control" id="full-name" name="full-name"
placeholder="John Smith">
<small class="message" id="message-full-name"></small>
<br>
</div>
<div class="mb-3 input-control">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email"
placeholder="[email protected]">
<small class="message" id="message-email"></small>
<br>
</div>
<div class="mb-3 input-control">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password"
placeholder="Password">
<small class="message" id="message-password"></small>
<br>
</div>
<div class="mb-3 input-control">
<label for="pwdRepeat">Password repeat</label>
<input type="password" class="form-control" id="pwdRepeat" name="pwdRepeat"
placeholder="Retype Password">
<small class="message" id="message-pwdRepeat"></small>
<br>
</div>
<a href="./includes/reset-password-form.php">Forgot your password?</a>
<div class="modal-footer">
<button type="reset" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" id="submit" class="btn btn-primary" name="submit">Register now</button>
</div>
<script src="./js/signup_error_handler.js"></script>
</form>
注册表单的 Javascript
const form = document.getElementById('signup-form');
const name = document.getElementById('full-name');
const email = document.getElementById('email');
const password = document.getElementById('password');
const password_repeat = document.getElementById('pwdRepeat');
form.addEventListener('submit', e => {
if (validateInputs()) {
e.currentTarget.submit();
} else {
e.preventDefault();
}
});
function validateInputs() {
//Get the value from inputs
const nameValue = name.value.trim();
const emailValue = email.value.trim();
const passwordValue = password.value.trim();
const passwordRepeatValue = password_repeat.value.trim();
let return_value = false;
//These variables are set with one when the value of the input field is correct
let name_check = 0;
let email_check = 0;
let password_check = 0;
let password_repeat_check = 0;
if (nameValue === '') {
//Show error and set error class
setError(name, 'Your name cannot be empty');
} else {
//Add success class
setSuccess(name);
name_check = 1;
}
if (emailValue === '') {
//Show error and set error class
setError(email, 'Email field cannot be empty');
} else if (!isEmail(emailValue)) {
setError(email, 'Email is not valid');
} else {
//Add success class
setSuccess(email);
email_check = 1;
}
if (passwordValue === '') {
//Show error and set error class
setError(password, 'Password field cannot be empty');
} else if (passwordValue.length <= 6) {
setError(password, 'Please enter a longer password');
} else {
//Add success class
setSuccess(password);
password_check = 1;
}
if (passwordRepeatValue === '') {
//Show error and set error class
setError(password_repeat, 'Password repeat field cannot be empty');
} else if (passwordValue !== passwordRepeatValue) {
setError(password_repeat, 'The passwords do not match');
}else if (passwordRepeatValue.length <= 6){
setError(password_repeat,"Repeated password needs to be longer")
} else {
//Add success class
setSuccess(password_repeat);
password_repeat_check = 1;
}
if (name_check === 1 && email_check === 1 && password_check === 1 && password_repeat_check === 1) {
return_value = true;
} else {
return_value = false;
}
return return_value;
}
function setError(element, message) {
element.className = "form-control error";
const small = document.getElementById("message-" + element.id);
small.classList.remove('success');
//Add error message and icon
small.innerHTML = message + ' <i class="fas fa-exclamation-circle">';
//Add error class
small.classList.add("error");
}
const setSuccess = (element) => {
element.className = "form-control success";
const small = document.getElementById("message-" + element.id);
small.classList.remove('error');
//Add success icon
small.innerHTML = '<i class="fas fa-check-circle">';
//Add success class
small.classList.add("success");
}
function isEmail(email) {
return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}
登录表单的 Html(不能像它应该的那样工作)
<form id="login-form" action="./includes/login.inc.php" method="POST">
<label for="login-email" class="call-form-label">
Email:
</label>
<input type="email" class="form-control" name="email" id="login-email" placeholder="Email">
<small class="message" id="message-login-email"></small>
<br>
<label for="login-password" class="call-form-label">
Password:
</label>
<input type="password" class="form-control" name="password" id="login-password" placeholder="Password">
<small class="message" id="message-login-password"></small>
<br>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="submit" name="login-submit" id="login-submit" class="btn btn-primary">Login</button>
<hr>
<p>Did not receive Your Verification Email?
<a href="./resend-email-verification.php">Resend</a>
</p>
</div>
</form>
登录表单的 Javascript
const login_form = document.getElementById('login-form');
const login_email = document.getElementById('login-email');
const login_password = document.getElementById('login-password');
login_form.addEventListener('submit', e => {
if (login_validateInputs()) {
e.currentTarget.submit();
} else {
e.preventDefault();
}
});
function login_validateInputs() {
//Get the value from inputs
const login_email_value = login_email.value.trim();
const login_password_value = login_email.value.trim();
//These variables are set with one when the value of the input field is correct
let login_email_check = 0;
let login_password_check = 0;
if (login_email_value === '') {
//Show error and set error class
login_setError(login_email, "Email field cannot be empty");
} else if (!login_isEmail(login_email_value)) {
login_setError(login_email, "Email is not valid");
} else {
//Add success class
login_setSuccess(login_email);
login_email_check = 1;
}
if (login_password_value === '') {
//Show error and set error class
login_setError(login_password, 'Password field cannot be empty');
} else if (login_password_value.length <= 6) {
login_setError(login_password, 'Please enter a longer password');
} else {
//Add success class
login_setSuccess(password);
login_password_check = 1;
}
if (login_password_check === 1 && login_email_check === 1) {
return_value = true;
} else {
return_value = false;
}
return return_value;
}
function login_setError(element, message) {
element.className = "form-control error";
const small = document.getElementById("message-" + element.id);
small.classList.remove('success');
//Add error message and icon
small.innerHTML = message + ' <i class="fas fa-exclamation-circle" >';
//Add error class
small.classList.add("error");
}
const login_setSuccess = (element) => {
element.className = "form-control success";
const small = document.getElementById("message-" + element.id);
small.classList.remove('error');
//Add success icon
small.innerHTML = '<i class="fas fa-check-circle">';
//Add success class
small.classList.add('success');
}
function login_isEmail(email) {
return /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(email);
}
有没有我没有注意到的明显错误?
我遇到的另一个问题是,当用户输入有效的值时,表单实际上并没有提交任何值,因为我有服务器端php错误处理程序,这些处理程序在提交表单后未激活,应该激活。例如,php错误处理程序会检查输入的电子邮件是否已经在数据库中,并且即使我使用了已经使用的电子邮件地址,也没有激活此错误处理程序。由于我在检查输入值 preventDefault() 和 currentTargetSubmit() 之前用于阻止表单提交,因此我应该将 currentTargetSubmit() 用于表单的所有输入值吗?
答:
0赞
Janosz Крајишник
4/28/2022
#1
答案其实很简单:我只是忘了从注册模式的 javacript 代码中的变量更改登录模式的 javascript 代码中的变量
评论
required
match
minlength