Press virtual keyboard buttons according to password

Asked

Viewed 159 times

0

I made a virtual keyboard that generates the numbers and returns in json, for example:

            [0] => Array
                (
                    [a] => 1
                    [b] => 5
                )

            [1] => Array
                (
                    [a] => 6
                    [b] => 7
                )

            [2] => Array
                (
                    [a] => 4
                    [b] => 9
                )

            [3] => Array
                (
                    [a] => 8
                    [b] => 2
                )

            [4] => Array
                (
                    [a] => 3
                    [b] => 0
                )

The above code generates the numbers in the following format: A or B For example: Press this button if your password is between A or B.

And along with this code, there is a variable that defines the person’s password, in which case the password is 011232. The values that the virtual keyboard gives are random.

I need a code that picks the values between the 5 buttons and check button by button, if the variable’s password equals one of the two values (A and B)

Thanks in advance.

  • Note that the code quoted in the question nay is JSON, but a literal of array PHP. Also, your question is not clear. You want the index sequence of array keyboard principal that corresponds to what a user would press when using the keyboard to enter the password?

1 answer

0

I rode in JS

var teclas = [{"a":1,"b":5},{"a":6,"b":7},{"a":4,"b":9},{"a":8,"b":2},{"a":3,"b":0}];
var password = '011232';
var check = 0;

function checkPassword(e){
    var index = parseInt(this.getAttribute('data-index'));
    var arr = [parseInt(teclas[index].a), parseInt(teclas[index].b)];
    if(arr.indexOf(parseInt(password[check])) != -1){
        check++;
    }else{
        check = 0;
        alert('Password Error');
    }

    if(check >= password.length){
        alert('Password OK');
    check = 0;
    }
}

function createButton(i){

    var o = teclas[i];

    var button = document.createElement('input');
    button.type = 'button';
    button.value = o.a+','+o.b;
    button.setAttribute('data-index', i);
    button.addEventListener('click', checkPassword);

    return button;
}

var teclado = document.getElementById('teclado');
for(var i=0; i < teclas.length; i++){
    teclado.appendChild(createButton(i));
}
<div id="teclado">
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.