locked
I need to disable 0 only at first position in text box using java script in ASP.NET RRS feed

  • Question

  • User1878568433 posted

    I need to disable 0 only at first position in text box using java script in ASP.NET

    Tuesday, March 26, 2019 12:00 PM

All replies

  • User839733648 posted

    Hi sripadasatpathy,

    I suggest that you could use regular expression and .test() function to achieve your requirement.

    Here is my testing demo.

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <script>
            $(function () {
                $("#test").on("input", function () {
                    if (/^0/.test(this.value)) {
                        this.value = this.value.replace(/^0/, "")
                    }
                })
            })
        </script>
    </head>
    <body>
        <input type="text" id="test">
    </body>
    </html>

    Best Regards,

    Jenifer

    Wednesday, March 27, 2019 2:58 AM
  • User-1151440187 posted

    I've done with this code : 

    $(document).ready(function()
    {
    $('#target').keyup(function() {
    var myLength = $("#target").val().length;
    if(myLength == 1) 
    {
    if($(this).val() === '0')
    {
    //alert('0 is not allowed as first character');
    $(this).val('');
    }
    }
    });

    Html code is :

    <form>
    <input id="target" type="textbox"/>
    </form>

    This will not accept 0 at first position and when input 0 at first position then it will remove it.

    I hope this will help you.

    Friday, March 20, 2020 5:34 AM