简体   繁体   中英

How to make a input box like Number seperator in html?

I would like to make this type input box. But I don't know the exact html code.

If you want fully working code, then you may need to use CSS and Javascript along with html. I have written a sample code which contains 10 digits of input and a button. It does the below three functionalities.

1.Each digit allows only numbers and length of a digit is 1. It means you can not enter more than one digit.

2.Once you enter a digit the cursor automatically moves to next digit.

3.On clicking on the button (Get Phone Number) you get the entered phone number as alert.

You can change the code as per your need.

Code:

<!DOCTYPE html>
<html>
<style type="text/css">
table,td{
 border-collapse: collapse;
 padding:0;
 border:2px solid black;
}
td input{
width:20px;
}

</style>

<script type="text/javascript">
var currentDigit = 1;
function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function moveToNextInputDigit(e){
     currentDigit = parseInt(e.id.replace("digit",""));
     currentDigit++; 
     if(e.value.length == 1 && currentDigit<=10 && document.getElementById("digit"+currentDigit).value.length!=1)
     {
      document.getElementById("digit"+currentDigit).focus();
     }
}

function getPhoneNumber()
{
 var phoneNumber = "";
 for(var i=1;i<=10;i++)
 {
    phoneNumber += document.getElementById("digit"+i).value;
 }
 
  alert("Phone Number: "+ phoneNumber);
}

</script>

<body>

<table>
<tr>
<td><input id="digit1" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit2" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td> 
<td><input id="digit3" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit4" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit5" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td> 
<td><input id="digit6" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit7" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit8" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit9" type="text" maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
<td><input id="digit10" type="text"maxlength="1" onkeypress="return isNumberKey(event)" onkeyup ="moveToNextInputDigit(this)"></td>
</tr> 
</table>

<input type = "button" value="Get Phone Number" onClick="getPhoneNumber()">
</body>
</html>

Output:

在此处输入图片说明

You can see working code here : https://jsfiddle.net/sampathcse16/m4m4kty8/4/

You can easily implement this using border , span and CSS ,

<div class="main-container">
    <span class="inner-container">
        <input class="input-style" type="text" maxlength="1" value="2">
    </span>
    <span class="inner-container">
        <input class="input-style" type="text" maxlength="1" value="2">
    </span>
    <span class="inner-container">
        <input class="input-style" type="text" maxlength="1" value="2">
    </span>
    <span class="inner-container">
        <input class="input-style" type="text" maxlength="1" value="2">
    </span>
    <span class="inner-container">
        <input class="input-style" type="text" maxlength="1" value="2">
    </span>
    <span class="inner-container">
        <input class="input-style" type="text" maxlength="1" value="2">
    </span>
    <span class="inner-container">
        <input class="input-style" type="text" maxlength="1" value="2">
    </span>
</div>

.main-container{
    border: 2px solid #000;
    width:180px;
}
.inner-container{
    border: 1px solid #000;
}
.input-style{
    width:20px;
    text-align:center;
    border:0px;
}

Demo : https://jsfiddle.net/yLd8k5zk/

NOTE : Width of main-container= width of inner-container*no of inner-container

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM