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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.