简体   繁体   English

jQuery密码(4位数字)验证

[英]Jquery passcode (4 digits number) validation

I need to validate a simple pin/passcode form and then be able to submit if correct . 我需要验证一个简单的密码/密码表单,然后如果正确就可以提交。

How can I validate this pin number If passocode is wrong display a message. 如何验证此密码:如果密码错误,则显示一条消息。 if passcode is correct then eneble button and proceed! 如果密码正确,那么按一下按钮并继续! Any help would be much appreciated. 任何帮助将非常感激。

HTML: HTML:

<form>
    <input type="password" maxlength="1" id="" name="pass" />
    <input type="password" maxlength="1" id="" name="pass" />
    <input type="password" maxlength="1" id="" name="pass" />
    <input type="password" maxlength="1" id="" name="pass" />
    <input type="submit" value="submit" id="submit" disabled="disabled" />
</form>

JS: JS:

 $("form").validate({
        rules: {
            pass: {
                required: true,
                minlength: 4,
                equalTo: "0000"
            }
        },
        messages: {
            pass: {
                required: "Please provide a PIN DIGIT",
                minlength: "Please enter at least 4 numbers"
            }
        }
    });

http://jsfiddle.net/E4Gz6/ http://jsfiddle.net/E4Gz6/

Well for this you will have to use jQuery Ajax. 为此,您将不得不使用jQuery Ajax。

Make a simple PHP script, which will take POST data, which will be your password, and if the password matches, for example with your database, or directly hardcoded to the PHP script, it will echo true otherwise false. 制作一个简单的PHP脚本,该脚本将获取POST数据,这将是您的密码,并且如果该密码与您的数据库匹配(例如,与您的数据库匹配)或直接硬编码到PHP脚本中,它将回显true或false。 If you wont use PHP, and you will store the PIN in your Javascript, it will be visible for others. 如果您不使用PHP,并且将PIN存储在Javascript中,则其他人将可以看到它。

EDIT: This should be working according to your design. 编辑:这应该根据您的设计工作。

<form>
<input type="password" maxlength="1" id="digit1" class="pass" />
<input type="password" maxlength="1" id="digit2" class="pass" />
<input type="password" maxlength="1" id="digit3" class="pass" />
<input type="password" maxlength="1" id="digit4" class="pass" />
<input type="submit" value="submit" id="submit" disabled="disabled" />
</form>

JS: JS:

$( document ).ready(function() {

 $(".pass").keydown( function() {
 pass=$("#digit1").val() + $("#digit2").val() + $("#digit3").val() + $("#digit4").val();
  $.ajax({
   type: "POST",
   url: "passCheck.php",
    data: "password="+pass,
    success: function(html){    
    if(html=='true')    {
         $("#submit").removeAttr("disabled");
    }
    else    {
         $("#submit").attr('disabled','disabled');

    }
   },

  });
});   

}); });

PHP: (just in case) PHP :(以防万一)

$pass = $_POST["password"];

if ($pass == "1234")
echo "true";
else
echo "false";

EDIT 2: If you dont want to use PHP and Ajax, simply for prototyping and playing with it, you can use simple check, but its strongly not recommanded, as everyone can see the pass in the code: 编辑2:如果您不想使用PHP和Ajax,只是为了进行原型设计和使用它,则可以使用简单的检查,但是强烈建议不要使用它,因为每个人都可以在代码中看到密码:

JS: JS:

$( document ).ready(function() {

 $(".pass").keydown( function() {
 pass=$("#digit1").val() + $("#digit2").val() + $("#digit3").val() + $("#digit4").val();

 correctPass = "1234";  
    if(pass==correctPass)    {
         $("#submit").removeAttr("disabled");
    }
    else    {
         $("#submit").attr('disabled','disabled');
    }
   });
});

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

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