It would be quite interesting if you provide an option to enter numbers using an on-screen number pad in your website. Since most of the new mobile devices doesn't come with a separate number pad, it is difficult to enter numbers fast. So providing an on-screen number pad would be a good alternative.
Here I have described a simple on-screen number pad developed using CSS, HTML and JavaScript.
Description
The number pad described here consist of twelve buttons. Ten of them represents each digit and the last two represents backspace and clear. They are arranged as seen in a typical number pad. A screen shot of the on-screen number pad is shown below.
Functions
To implement the on-screen number pad, three JavaScript functions are used. Each of them are described below. All the digits entered using the key pad is displayed in a textbox with id 'number'.
number_write(x)
This function is called every time a button representing a number is clicked. The argument 'x' denotes the number pressed. This function appends the number to the textbox.
number_clear()
This function clears all the data in the textbox. This function is called when the 'Clr' button on the number pad is clicked.
number_c()
This function deletes the last digit entered. It is similar to the backspace button in the keyboard. This function is called whenever the 'C' button on the on-screen keyboard is clicked.
Code
The complete code (CSS + HTML + JavaScript) is given below. It can be copied to a text editor and saved with '.html' file extension and opened using a web browser to see the results.
<html>
<head>
<title>On Screen Number Pad Using JavaScript</title>
<script type="text/javascript">
function number_write(x)
{
var text_box = document.getElementById("number");
if(x>=0 && x<=9)
{
if(isNaN(text_box.value))
text_box.value = 0;
text_box.value = (text_box.value * 10)+x;
}
}
function number_clear()
{
document.getElementById("number").value = 0;
}
function number_c()
{
var text_box = document.getElementById("number");
var num = text_box.value;
var num1 = num%10;
num -= num1;
num /= 10;
text_box.value = num;
}
</script>
<style type="text/css">
.main_panel
{
width:270px;
height:470px;
background-color:#999999;
border-top-right-radius:20px;
border-top-left-radius:20px;
border-bottom-right-radius:20px;
border-bottom-left-radius:20px;
padding:20px;
}
.number_button
{
width:70px;
height:70px;
margin:10px;
float:left;
background-color:#FFFF99;
border-top-right-radius:20px;
border-top-left-radius:20px;
border-bottom-right-radius:20px;
border-bottom-left-radius:20px;
font-size:36px;
text-align:center;
}
.number_button:hover
{
background-color:#66FF66;
}
.text_box
{
width:250px;
height:30px;
font-size:24px;
text-align:right;
}
</style>
</head>
<body>
<div class="main_panel">
<br />
<center><input class="text_box" type="text" id="number" name="num" /></center>
<br /><br />
<div class="number_button" onclick="number_write(1);">1</div>
<div class="number_button" onclick="number_write(2);">2</div>
<div class="number_button" onclick="number_write(3);">3</div>
<div class="number_button" onclick="number_write(4);">4</div>
<div class="number_button" onclick="number_write(5);">5</div>
<div class="number_button" onclick="number_write(6);">6</div>
<div class="number_button" onclick="number_write(7);">7</div>
<div class="number_button" onclick="number_write(8);">8</div>
<div class="number_button" onclick="number_write(9);">9</div>
<div class="number_button" onclick="number_clear();">Clr</div>
<div class="number_button" onclick="number_write(0);">0</div>
<div class="number_button" onclick="number_c();">C</div>
</div>
</body>
</html>
Here I have described a simple on-screen number pad developed using CSS, HTML and JavaScript.
Description
The number pad described here consist of twelve buttons. Ten of them represents each digit and the last two represents backspace and clear. They are arranged as seen in a typical number pad. A screen shot of the on-screen number pad is shown below.
Functions
To implement the on-screen number pad, three JavaScript functions are used. Each of them are described below. All the digits entered using the key pad is displayed in a textbox with id 'number'.
number_write(x)
This function is called every time a button representing a number is clicked. The argument 'x' denotes the number pressed. This function appends the number to the textbox.
number_clear()
This function clears all the data in the textbox. This function is called when the 'Clr' button on the number pad is clicked.
number_c()
This function deletes the last digit entered. It is similar to the backspace button in the keyboard. This function is called whenever the 'C' button on the on-screen keyboard is clicked.
Code
The complete code (CSS + HTML + JavaScript) is given below. It can be copied to a text editor and saved with '.html' file extension and opened using a web browser to see the results.
<html>
<head>
<title>On Screen Number Pad Using JavaScript</title>
<script type="text/javascript">
function number_write(x)
{
var text_box = document.getElementById("number");
if(x>=0 && x<=9)
{
if(isNaN(text_box.value))
text_box.value = 0;
text_box.value = (text_box.value * 10)+x;
}
}
function number_clear()
{
document.getElementById("number").value = 0;
}
function number_c()
{
var text_box = document.getElementById("number");
var num = text_box.value;
var num1 = num%10;
num -= num1;
num /= 10;
text_box.value = num;
}
</script>
<style type="text/css">
.main_panel
{
width:270px;
height:470px;
background-color:#999999;
border-top-right-radius:20px;
border-top-left-radius:20px;
border-bottom-right-radius:20px;
border-bottom-left-radius:20px;
padding:20px;
}
.number_button
{
width:70px;
height:70px;
margin:10px;
float:left;
background-color:#FFFF99;
border-top-right-radius:20px;
border-top-left-radius:20px;
border-bottom-right-radius:20px;
border-bottom-left-radius:20px;
font-size:36px;
text-align:center;
}
.number_button:hover
{
background-color:#66FF66;
}
.text_box
{
width:250px;
height:30px;
font-size:24px;
text-align:right;
}
</style>
</head>
<body>
<div class="main_panel">
<br />
<center><input class="text_box" type="text" id="number" name="num" /></center>
<br /><br />
<div class="number_button" onclick="number_write(1);">1</div>
<div class="number_button" onclick="number_write(2);">2</div>
<div class="number_button" onclick="number_write(3);">3</div>
<div class="number_button" onclick="number_write(4);">4</div>
<div class="number_button" onclick="number_write(5);">5</div>
<div class="number_button" onclick="number_write(6);">6</div>
<div class="number_button" onclick="number_write(7);">7</div>
<div class="number_button" onclick="number_write(8);">8</div>
<div class="number_button" onclick="number_write(9);">9</div>
<div class="number_button" onclick="number_clear();">Clr</div>
<div class="number_button" onclick="number_write(0);">0</div>
<div class="number_button" onclick="number_c();">C</div>
</div>
</body>
</html>