Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS: Create iOS style Pin Password box

Tags:

html

css

I want to create a html5 input that looks like the iOS enter Pin (on the lock screen).... with 4 input boxes.

How do i achieve this?

enter image description here

I tried the following:

1) Created a normal input box.

2) Removed its borders.

3) Placed the above image as the background of the input box.

4) And added some letter spacing.

Problem is: As i type the first 3 characters, it fits in the boxes:

enter image description here

But when i type the 4th character, the characters move towards the left and hence it appears as below:

enter image description here

What can I do to fix this ?

Any other better approach ??

EDIT:

Ok based on the below answers, I modified the approach to have 4 input boxes. And on keyup event for each input box, I change the focus to next input box.

This works well on browser. But does not work on the device (iPhone 5). What is the problem ?

It is a Sencha Touch app packaged using Cordova.

SOLVED:

Need to disable <preference name="KeyboardDisplayRequiresUserAction" value="false" />

like image 711
senchaDev Avatar asked Nov 21 '13 06:11

senchaDev


4 Answers

Instead of images, have 4 input boxes. Fiddle here: http://jsfiddle.net/JLyn9/2/

<input type="password" maxlength=1 id="1" onkeyup="moveOnMax(this,'a')" />
<input type="password" maxlength=1 id="a" onkeyup="moveOnMax(this,'b')" />
<input type="password" maxlength=1 id="b" onkeyup="moveOnMax(this,'c')" />
<input type="password" maxlength=1 id="c" />

moveOnMax =function (field, nextFieldID) {
    if (field.value.length == 1) {
        document.getElementById(nextFieldID).focus();
    }
}

PS the JS function can be optimized.

EDIT/WARNING: This is not a solution to this problem. Please look at the other answers

like image 160
stackErr Avatar answered Oct 09 '22 12:10

stackErr


Instead of using background-image for this purpose, use four input boxes with their type set to password, and than use jQuery to achieve some more user friendliness, if you don't need that functionality of auto focusing to next field you can simply omit the script out and achieve your style..

Demo

input[type=password] {
    padding: 10px;
    border: 1px solid #ddd;
    width: 50px;
    height: 50px;
    text-align: center;
    font-size: 30px;
}

jQuery (Optional for auto focus functionality)

$("input").keyup(function() {
    if($(this).val().length >= 1) {
      var input_flds = $(this).closest('form').find(':input');
      input_flds.eq(input_flds.index(this) + 1).focus();
    }
});
like image 43
Mr. Alien Avatar answered Oct 09 '22 11:10

Mr. Alien


No need for jQuery, much cleaner IMO.

HTML

<form id="ios">
    <input type="password" maxlength="1" />
    <input type="password" maxlength="1"/>
    <input type="password" maxlength="1"/>
    <input type="password" maxlength="1"/>
</form>   

CSS

form#ios input[type=password]{
    height: 50px;
    width: 40px;
    text-align: center;
    font-size: 2em;
    border: 1px solid #000;
} 

Demo

http://jsfiddle.net/jerryhuangme/e9yhr/

like image 4
JerryHuang.me Avatar answered Oct 09 '22 13:10

JerryHuang.me


To keep the style clean, I would suggest you to create 4 input boxes.

Write a small script to focus the next input box when user enters character in the current input box.

like image 2
aBhijit Avatar answered Oct 09 '22 12:10

aBhijit