[英]How do I validate my form before redirecting to another page?
我正在创建一个注册表单,我希望在将用户发送到 login.html 页面之前对其进行验证。
有没有办法用这段代码做到这一点? 我希望所有这些字段都超过 2 个字符,现在在#firstName
字段中写入 2 个字符就足够了,然后它会重定向。
$('#regForm').on('submit', function() {
if($('#firstName, #lastName, #address, #city, #zip, #phoneNumber, #email, #inputPassword, #confirmPassword').val().length >= 2)
{
window.location.replace("login.html");
} else {
alert('You must fill in all the required fields.')
}
})
您可以在回调函数中使用event.preventDefault()
:
$('#regForm').on('submit', function(event) {
event.preventDefault();
if($('#firstName, #lastName, #address, #city, #zip, #phoneNumber, #email, #inputPassword, #confirmPassword').val().length >= 2)
{
window.location.replace("login.html");
}
else {
alert('You must fill in all the required fields.');
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.