Jump to content

[SOLVED] Ajax Password checker help?


darkfreaks

Recommended Posts

okay guys i have currently an application that will check the strength of your password using Ajax and PHP.

 

 

i need help on how to make it work with a submit button. code is below.

 

Ajax.php:

<?php
$do = $_GET['do'];
switch($do) {
    case 'check_password_strength':
        $password = $_GET['pass'];
        $strength = 0;
        // letters (lowercase)
        if(preg_match("/([a-z]+)/", $password)) {
            $strength++;
        }
        // letters (uppercase)
        if(preg_match("/([A-Z]+)/", $password)) {
            $strength++;
        }
        // numbers
        if(preg_match("/([0-9]+)/", $password)) {
            $strength++;
        }
        //underscores&dashes
        if(preg_match("/([_-])/", $password)) {
            $strength++;
        }
        
        header('Content-Type: text/xml');
        header('Pragma: no-cache');
        echo '<?xml version="1.0" encoding="UTF-8"?>';
        echo '<result><![CDATA[';
        switch($strength) {
            case 1:

            	    echo '<div style="width: 25%" id="password_red">Hackable</div>';
            break;
            case 2:
            	    echo '<div style="width: 50%" id="password_yellow">Not Secure</div>';
            break;
            case 3:
            	    echo '<div style="width: 75%" id="password_green">Secure</div>';
            break;
            case 4:
            	    echo '<div style="width: 100%" id="password_green">Very Secure</div>';
            break;
        }
        echo ']]></result>';
    break;
    default:
        echo 'Error, invalid action';
    break;
}
?> 

passcheck.php:

<script type="text/javascript">
function toggle_pass(passid) {
    if (window.XMLHttpRequest) {
        http = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    handle = document.getElementById(passid);
    var url = 'ajax.php?';
    if(handle.value.length > 0) {
        var fullurl = url + 'do=check_password_strength&pass=' + encodeURIComponent(handle.value);
        http.open("GET", fullurl, true);
        http.send(null);
        http.onreadystatechange = statechange_password;
    }else{
        document.getElementById('password_strength').innerHTML = '';
    }
}

function statechange_password() {
    if (http.readyState == 4) {
        var xmlObj = http.responseXML;
        var html = xmlObj.getElementsByTagName('result').item(0).firstChild.data;
        document.getElementById('password_strength').innerHTML = html;
    }
}
</script>
<input id="pass" type="password" name="password" onchange="toggle_pass('pass')" /><br /><br />
<strong><span id="text2">Password Security</span></strong>:<br />
<div id="password_strength"> </div>

Link to comment
https://forums.phpfreaks.com/topic/173409-solved-ajax-password-checker-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.