Javascript 在重定向到其他页面之前,如何验证表单?

Javascript 在重定向到其他页面之前,如何验证表单?,javascript,forms,validation,redirect,Javascript,Forms,Validation,Redirect,我正在创建一个注册表单,希望在它将用户发送到login.html页面之前对其进行验证 有没有办法用这段代码做到这一点?我希望所有这些字段都超过2个字符,现在在#firstName字段中写入2个字符就足够了,然后它会重定向 $('#regForm').on('submit', function() { if($('#firstName, #lastName, #address, #city, #zip, #phoneNumber, #email, #inputPassword, #

我正在创建一个注册表单,希望在它将用户发送到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.');
   }
});

在#firstName字段中输入超过2个字符后,它仍会重定向,而其他字段为空。请逐个检查:
$('#firstName').val().length>=2&&$('#lastName').val().length>=2&&…
尝试逐个检查,但仍然无效,现在我只需单击submit按钮就可以重定向,而不必填充任何字段。因此,这个if语句应该可以正常工作:
if($('firstName').val().length>=2&&$('lastName').val().length>=2&&$('#address').val().length>=2&$('city').val().length>=2&$('chip').val().length>=2&$('phoneNumber').val().length>=2&&$('#email').val().length>=2$('#inputPassword').val().length>=2&&$('#confirmPassword').val().length>=2)
谢谢!我一定错过了什么,也许,这很好。谢谢你的帮助!:)