简体   繁体   English

如何在 jQuery 中验证表单?

[英]How to validate a form in jQuery?

I want to validate my data with jQuery or Javascript and send them to the server but why aren't they validated?我想用 jQuery 或 Javascript 验证我的数据并将它们发送到服务器,但为什么它们没有被验证?

 $(document).ready(function() { var name = $('#signup-name').val(); var email = $('#signup-email').val(); var password = $('#signup-password').val(); var email_regex = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,4}$/i); var pass_regex = new RegExp(/^(?=.[0-9])(?=.[!@#$%^&])[a-zA-Z0-9!@#$%^&]{7,15}$/); $('#signup-form').on('submit', function(e) { e.preventDefault(); if (validate()) { $.ajax({ type: 'post', url: 'signup', data: { email: email, password: password, name: name }, }); } else { return false; }; }); function validate() { // name cheak here if (name.length == "") { $('.nameerror').html("Name field required !"); return false; } else if (name.length = < 3) { $('.nameerror').html("Name Should be greater than 3"); return false; }; // email cheak here if (email.length == "") { $('.emailerror').html("Email field required !"); return false; } else if (!email_regex.test(email)) { $('.emailerror').html("Please enter correct email."); return false; }; // password cheak here if (password.length == "") { $('.passerror').html("password field required !"); return false; } else if (!pass_regex.test(password)) {# ('.passerror').html("Minimum eight characters, at least one letter and one number:"); return false; }; }; });

There are two major issues, you were just not passing the arguments to the validate function.有两个主要问题,您只是没有将参数传递给验证函数。 I have updated your code with arguments passed to the function.我已经使用传递给函数的参数更新了您的代码。

Furthermore, you never returned true for any function as a result nothing would be returned.此外,您从未为任何函数返回 true,因此不会返回任何内容。 Also your if statements are split and will contradict.此外,您的 if 语句是分开的,并且会相互矛盾。

I have corrected these issues, hopefully this should work!我已经纠正了这些问题,希望这应该有效!

$(document).ready(function() { 
  $('#signup-form').on('submit', function(e) {
  var name = $('#signup-name').val();
  var email = $('#signup-email').val();
  var password = $('#signup-password').val();
    e.preventDefault();
    if (validate(name, email, password)) {

      $.ajax({
        type: 'post',
        url: 'signup',
        data: {
          email: email,
          password: password,
          name: name
        },
      });

    } else {
      return false;
    };
  });
});


function validate(name, email, password) {
  var email_regex = new RegExp(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i);
  var pass_regex = new RegExp(/^(?=.[0-9])(?=.[!@#$%^&])[a-zA-Z0-9!@#$%^&]{7,15}$/);
  // name cheak here
  if (name.length == 0) {
    $('.nameerror').html("Name field required !");
    return false;
  } else if (name.length <= 3) {
    $('.nameerror').html("Name Should be greater than 3");
    return false;
  } else if (email.length == 0) { //Check Email
    $('.emailerror').html("Email field required !");
    return false;
  } else if (!email_regex.test(email)) {
    $('.emailerror').html("Please enter correct email.");
    return false;
  } else if (password.length == 0) { // password cheak here
    $('.passerror').html("password field required !");
    return false;
  } else if (!pass_regex.test(password)) {
    ('.passerror').html("Minimum eight characters, at least one letter and one number:");
    return false;
  } else {
    return true;
  }

};

I believe the issue is that, although the validate function does indeed have access to the variables name etc, these are just set once when the document is first ready, and never updated.我认为问题在于,虽然validate函数确实可以访问变量name等,但这些只是在文档第一次准备好时设置一次,并且永远不会更新。 The values of the variables should be set inside the event handler for the submit event, before validate is called.在调用validate之前,应在submit事件的事件处理程序中设置变量的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM