doupingtang9627 2014-08-17 11:46
浏览 22
已采纳

检查用户名时,jQuery Validate Plugin会引发错误

JSFIDDLE: http://jsfiddle.net/c56z1674/ (error explanation at the end of this post)

My HTML (for simplification purposes I only included username field):

<form id="form_register_create" method="get" action="">
    <input type="text" id="register_username" name="register_username" placeholder="username" autocomplete="off" autofocus required>
    <input type="submit" id="form_register_create_submit" style="display:none">    
</form>

My JS:

$('#form_register_create').validate({
    // specify rules
    rules : {
        register_username : {
            required : true,
            register_username : true,
            minlength : 3,
            maxlength : 18,
            remote : {
                url : "../system/check-uniqueness-username.php",
                type : "post"
            }
        }
    },
    // specify error messages
    messages : {
        register_username : {
            required : "Please enter a username.",
            register_username : "Please enter a username (between 3 and 18 chars).",
            remote : "Username already in use!"
        }
    },
    // when everything is valid, fire register function
    submitHandler : function(form) {
        register_create();
    }
});

My PHP (check-uniqueness-username.php):

<?php

require_once 'db.php';

$username = trim(strtolower($_REQUEST['register_username']));


$results = $db -> query("SELECT * FROM users WHERE username = $username");  

if(mysqli_num_rows($results) == 1) {
 $valid = 'false';
}
else{
 $valid = 'true';
} 


echo $valid;
exit;

THE FOLLOWING ERROR OCCURS:

When I enter the username in the field and leave the field by clicking into another field, I get the following error message in my console:

Uncaught TypeError: Cannot read property 'call' of undefined

What I found out: This error does NOT OCCUR if I leave out the part "register_username : true" in the rules section of my validate script. But leaving this part out is not an option for me because in this case I will not be able to check the uniqueness of the username.

Does anyone have any idea how this error can be avoided? You can try out yourself by typing something in the username field and then clicking into the email field: http://jsfiddle.net/c56z1674/

  • 写回答

1条回答 默认 最新

  • douchuoliu4422 2014-08-17 12:09
    关注

    I guess you were using the jQuery validation documentation example for this:

    $( "#myform" ).validate({
      rules: {
        email: {                         // <----- email(1)
          required: true,
          email: true,                   // <----- email(2)
          remote: "check-email.php"
        }
      }
    });
    

    The fieldnames are the same, but email(2) is a build in validation to check if the field contains a valid email. (Documentation)

    So in your code, just remove the 'register_username : true,' as it's not an available option and your validation script will make the php call.

    JSFiddle

    HTML:

    <form id="form_register_create" method="get" action="">
        <input type="text" id="register_username" name="register_username" placeholder="username" autocomplete="off" autofocus required>
        <input type="submit" id="form_register_create_submit" style="display:none">    
    </form>
    

    JS:

    $('#form_register_create').validate({
        // specify rules
        rules : {
            register_username : {
                required : true,
                minlength : 3,
                maxlength : 18,
                remote : {
                    url : "../system/check-uniqueness-username.php",
                    type : "post"
                }
            }
        },
        // specify error messages
        messages : {
            register_username : {
                required : "Please enter a username.",
                register_username : "Please enter a username (between 3 and 18 chars).",
                remote : "Username already in use!"
            }
        },
        // when everything is valid, fire register function
        submitHandler : function(form) {
            register_create();
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?