Skip to content

Instantly share code, notes, and snippets.

@roshanca
Last active August 29, 2015 14:04
Show Gist options
  • Save roshanca/d63ffcb4996e6f49572a to your computer and use it in GitHub Desktop.
Save roshanca/d63ffcb4996e6f49572a to your computer and use it in GitHub Desktop.
How to show numeric soft keyboard for password field in Android

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>
  1. add another input field in the #passwordCell div, and give it an id, for example, password_mask
  2. hide the #Password input field
  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();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment