4

I'm working on a mobile project for Android using PhoneGap, and testing with Samsung Galaxy S2 Skyrocket, android version 4.0.4 . Right now I have a html input field that only accepts numeric password, so first I set the input like this

<input type="password" id="Password"/>
it gives me the alphabetical keyboard in Android, but I want the show up keyboard to be numeric.

So I change my code to be
<input type="tel" id="Password"/> and use CSS to mask the password

<style type="text/css">
        #Password {
            -webkit-text-security: disc;
        }
</style>


but the masking is not working same as the type="password", it will mask the passwords when the password box is not getting focused. If it is getting focused, password will not be masked by disc, so it will be shown.

Is there any way I can make the numeric keyboard shown and let the password to be masked all the time?

Thanks.

Jerry Wu
  • 51
  • 1
  • 6

1 Answers1

1

I got this fixed

The password input tag is in a div like this

<div id="passwordCell" style="display:inline;">
    <input type="password" pattern="[0-9]*" name="Password" id="Password" placeholder="4 digit numeric only allowed" maxlength="4"/>
</div>

So I solved the the problem in the following steps:

  1. add another input field in the #passwordCell div, and give it an id, for example, password_mask <input type="tel" name="password_mask" id="password_mask" placeholder="4 digit numeric only allowed." maxlength="4"/>

  2. hide the #Password input field
    $("#Password").hide();

  3. bind a jQuery keyup event to #password_mask input field to make it pass its inputed value to #Password field, and replace the inputed character in #password_mask with *.
    I do this because at the end the value in #Password will be sent to server as user's password

Here's the javascript code, I used jQuery

$("#passwordCell").html('');
$("#passwordCell").append('<input type="tel" name="password_mask" id="password_mask" placeholder="4 digit numeric only allowed." maxlength="4"/>' +
                            '<input type="password" pattern="[0-9]*" name="Password" id="Password" placeholder="4 digit numeric only allowed" maxlength="4" data-validation-required="true" data-validation-pattern="pin not_empty"/>');
$("#password_mask").textinput();
$("#Password").textinput();
//$("#password_mask").show();
$("#Password").hide();
$("#password_mask").keyup(function() {
    var inputLength = $(this).val().length;
    var passwordLength = $("#Password").val().length;
    if (inputLength > passwordLength) {
        var inputLastChar = $(this).val().charAt(inputLength-1);
        $("#Password").val($("#Password").val() + inputLastChar);
    } else {
        $("#Password").val($("#Password").val().substring(0, $(this).val().length));
    }

    var i = 0;
    var maskPassword = "";
    while (i < $("#password_mask").val().length) {
        maskPassword += "*";
        i++;
    }
    $("#password_mask").val(maskPassword);
});
$("#password_mask").blur(function() {
    $("#Password").focus();
    $("#Password").blur();
});
Jerry Wu
  • 51
  • 1
  • 6