简体   繁体   English

验证jQuery函数的问题

[英]Problems with validate jquery function

I was trying to make a validation in my form with jquery, but it does not work the way it was supposed to and I have no idea why. 我试图用jquery在我的表单中进行验证,但是它不能按预期的方式工作,我也不知道为什么。

I have this function to make the validation: 我有此功能进行验证:

function newLogin () {
var username = $("#popup-login-email").val();
var password = $("#popup-login-password").val();
if (username == "" || password.length<5){

  $(document).ready(function () {

      $("#popup-login-form").validate({ // initialize the plugin

          rules: {
              email: {
                  required: true,
                  email: true
              },
              password: {
                  required: true,
                  minlength: 5
              }
          },

      });

  });

  return false;
}
else{
  Parse.User.logIn(username, password, {

  success:function(user){
        console.log("login successfull");
        if(checkEmail()){
          console.log(checkEmail());
          document.location.href = "Temas.html";
        }
  },
    error: function(user, error){
        console.log(error.message);
        displayErrorDiv();
    }
  })
}

} }

And i got this form 我得到了这张表格

<form id = "popup-login-form">
                <input type="email" name="email" placeholder="Email" id = "popup-login-email" class="popup-input first"/>
                <div id="error-message-email" class="error">

                </div>
                <input type="password" name="password" placeholder = "Password" id="popup-login-password" class="popup-input"/>
                <div id="error-message-password" class="error">

                </div>
                <button class="popup-button" id="popup-cancel">Cancel</button>
                <button type="submit" class="popup-button" id="popup-submit">Login</button>
                <div class="error-message-login" class="error">

                </div>
            </form>

And the weird part is that just does not work in my page. 而奇怪的是,这在我的页面中不起作用。 Here it works, for example: http://jsfiddle.net/xs5vrrso/ 它在这里有效,例如: http : //jsfiddle.net/xs5vrrso/

There is no problem with the code which you shared in jsfiddle but the above code you are using $(document).ready({function()}) inside a function which is of no use. 您在jsfiddle中共享的代码没有问题,但是上面的代码在没有用的$(document).ready({function()})中使用$(document).ready({function()}) Now the problem is that the method newLogin is not called on dom ready and thus this issue occurs. 现在的问题是在dom就绪时未调用方法newLogin,因此会发生此问题。

Better keep the function call inside $(document).ready({function() newLogin() }) . 最好将函数调用保留在$(document).ready({function() newLogin() }) Now you can also use submitHandler in validate to merge the if else conditions. 现在,您还可以在validate中使用submitHandler合并if else条件。

With jQuery when i get the 使用jQuery时,我得到了

"TypeError: $(...).validate is not a function" “ TypeError:$(...)。validate不是函数”

I change 我改变

$(..).validate $(..)。validate

for 对于

jQuery(..).validate jQuery(..)。验证

您必须在jquery文件之后包括此验证文件。

<script src="http://cdn.jsdelivr.net/jquery.validation/1.14.0/jquery.validate.js"></script>

i make one example to you 我给你一个例子

jsfiddler example jsfiddler示例

$(document).ready(function () {
      $("#popup-login-form").validate({ // initialize the plugin

          rules: {
              email: {
                  required: true,
                  email: true
              },
              password: {
                  required: true,
                  minlength: 5
              }
          },

      });
    //event listening onSubmit
    $('form').submit(function(event){

        var returnForm = true;
        var username = $("#popup-login-email").val();
        var password = $("#popup-login-password").val();

        //Make your validation here
        if (username == "" || password.length<5){
            returnForm = false;
        }

        return returnForm; //Submit if variable is true
    });

});

Do not wrap the code under the if condition with $(document).ready() . 不要在if条件下使用$(document).ready()包装代码。 Change the code to : 将代码更改为:

if (username == "" || password.length < 5){
    $("#popup-login-form").validate({ // initialize the plugin
      /*remaining code here*/
    });
}

Also it is a good habit to trim the spaces around any input that you accept from the users. 修剪用户接受的任何输入周围的空间也是一个好习惯。 For eg in your case please do the following: 对于例如您的情况,请执行以下操作:

var username = $.trim($("#popup-login-email").val());
var password = $.trim($("#popup-login-password").val());
/* $.trim() would remove the whitespace from the beginning and end of a string.*/

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

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