简体   繁体   English

如何使用AJAX和jQuery检查正确包含“ / + = @#^”的密码

[英]How to check passwords correctly containing “/+=@#^” using AJAX and jQuery

I currently have a php functionality for password checking that works flawlessly on php. 我目前有一个用于密码检查的php功能,可以在php上完美地工作。 However, When I try to return the result from AJAX, the "+" symbol is somehow not being detected. 但是,当我尝试从AJAX返回结果时,就无法检测到“ +”符号。

This is the php functionality: 这是php功能:

function pwd_contents($pwd, $notAllowedChar)
    {
        $pass_array = str_split($pwd);
        $notAllowedChar_array = str_split($notAllowedChar);

        foreach($notAllowedChar_array as $nChar)
        {
            if(in_array($nChar, $pass_array))
                return true;
        }
        return false;
    }

    if(isset($_POST['password']))//If a username has been submitted 
    {
        $pattern = '/\/\@\+\=\#\*/';
        $password = $_POST['password'];//Some clean up :)

        if(pwd_contents($password, $pattern))
            echo '1';
        else
            echo '0';
    }

And this is the ajax call that calls it: 这是调用它的ajax调用:

$.ajax({ //Make the Ajax Request
    type: "POST",
    url: "ajax_check_password.php", //file name
    data: 'password=' + password, //data
    success: function(server_response) {

        $("#password_status").ajaxComplete(function(event, request) {

            if (server_response.trim() == '0') //if ajax_check_password.php return value "0"
            {
                $("#password_status").html('<img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Password is valid. </font>  ');
                $("#btn-submit-2").html('<button type="submit"  class="btn btn-danger btn-lg btn-block"   id="b"  style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>');
                //add this image to the span with id "availability_status"
            } else if (server_response.trim() == '1') //if it returns "1"
            {
                $("#password_status").html('<img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#b11116" style="padding-top: 5px;"> Password is not valid. </font>');
                $("#btn-submit-2").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">CONTINUE</a>');
            }
        });
    }
});

How would I actually get the response to be return me the result that I want if the password contains "+" on it? 如果密码上包含“ +”,我该如何实际得到响应以返回我想要的结果?

An example would be "Megason+2983". 例如“ Megason + 2983”。 The result from the ajax call would give me "Password is not valid." ajax调用的结果将给我“密码无效”。

Because you are using the "URL encoded" method of providing your data to the AJAX POST: 因为您使用的是将数据提供给AJAX POST的“ URL编码”方法,所以:

data: 'password=' + password, //data

jQuery is treating the "+" in your password as a URL encoded character (it is a reserved character in URLs), and therefore it gets converted to its plaintext equivalent (a space character) before PHP gets a chance to process the POST. jQuery将密码中的“ +”视为URL编码字符(它是URL中的保留字符),因此jQuery在PHP有机会处理POST之前将其转换为等效的纯文本(空格字符)。

To fix it, change it to: 要修复它,请将其更改为:

data: {password: password}, //data

For future troubleshooting of AJAX requests: inspecting the request via developer tools in the browser would have probably pointed you to the solution pretty quickly. 为了将来对AJAX请求进行故障排除:通过浏览器中的开发人员工具检查请求可能会使您很快找到解决方案。

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

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