0

is there a way to display increment value display in 5 digit 0 when display on textbox, i tried puting 00000 on my javascript, inside modal i have textbox that display increment value, when i open my modal it display 1 not 00001, hope you can help me. advance thanks.

here is my sample code:

<input type="text" class="form-control"  name="id"  id="id" readonly style="width:45%; margin-top:30px;"  value=" <?php 
              include_once('connection.php');
               $sql="select max(id)
                from test2";
                $result = mysqli_query($conn,$sql);
                $row = mysqli_fetch_row($result);
                echo $row["0"];
                 ?> ">

and this is my javascript

<script>
function incrementValue()
{
    var value = parseInt(document.getElementById('id').value, 10);
    value = isNaN(value) ? 00000 :  value;
    value++;
    document.getElementById('id').value = value;
}
</script>
ace
  • 148
  • 1
  • 2
  • 16
  • Use this line to format your number. The code is for an reference so please modify as per your need. All you need is to use this statment. var formattedNumber = ("00000" + value++).slice(-5); https://stackoverflow.com/a/8043061/1964336 – jatinder bhola Jul 26 '18 at 13:59

2 Answers2

2

You have to add the zeros when converting the number back to a string:

 function incrementValue() {
    var value = parseInt(document.getElementById('id').value, 10);
    value = value || 0;
    value++;
    document.getElementById('id').value = ("00000" +  value).substr(-5);
 }
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

Your incrementValue function should be like this:

function incrementValue()
{
    var value = parseInt(document.getElementById('id').value, 10);
    value = new Array(6 - (value + '').length).join('0') + (value + 1);
    document.getElementById('id').value = value;
}
incrementValue();
<input id="id" value='23' />
Viktor Vlasenko
  • 2,332
  • 14
  • 16