Remove leading zeros from input type=number

前端 未结 7 2042
一个人的身影
一个人的身影 2021-02-07 01:17

I noticed that if i use the leading zeros are not removed. I also saw a lot of discussion on how keeping leading ze

相关标签:
7条回答
  • 2021-02-07 01:37

    I'm going to give an example with react usage. It uses state which store the value of the field. But, I think u can replace it with whatever variable you like.

    <input type='number' value={Number(this.state.myNumber).toString()}/>

    In my case, myNumber stores a year number. This way, the year 2018 (for example) won't be displayed mistakenly as 02018.

    0 讨论(0)
  • 2021-02-07 01:37

    Try this :

    <!DOCTYPE html>
    <html>
    <body>
    
    <input type="number" id="txtfield" /><button onclick="myFunction()">Try it</button>
    
    <script>
    function myFunction() {
        var myNumber = document.getElementById("txtfield").value;
        document.write(parseInt(myNumber ,10));
    }
    </script>
    
    </body>
    </html>
    
    0 讨论(0)
  • 2021-02-07 01:43

    You can try this.

        str1 = str.replace(/^0+/,'');
    
    0 讨论(0)
  • 2021-02-07 01:46

    Html input tags always return text, not numbers, even its content can be coerced to numerical format, dates, etc...

    So next you should convert that input to actual number format:

    parseInt(myNum); // If you expect an integer.
    parseFloat(myNum); // If you expect floating point number.
    
    0 讨论(0)
  • 2021-02-07 01:57

    add step="any" to your input tag. if this does not work on your browser, change type to type="tel"

    0 讨论(0)
  • 2021-02-07 01:59

    just use a regular expression like this

    textboxText= textboxText.replace(/^0+/, '')
    
    0 讨论(0)
提交回复
热议问题