最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to valid Numeric values onkeypress - Stack Overflow

programmeradmin1浏览0评论

I have this simple HTML document

<input type="text"  id="my_text" onkeypress="valid_numbers(event);" size="30"/>
<script type="javascript">
function valid_numbers(e)
{
        var key=e.which || e.KeyCode;
        if  ( key >=48 && key <= 57)
         // to check whether pressed key is number or not 
                return true; 
         else return false;
}
</script>

What I want is:

onkeypress of my_text if the pressed key is number allow otherwise deny writing of the character.

But the above code doesn't work, my_text element accept any character, what is wrong with the above code ?!..

Help Please!..

I have this simple HTML document

<input type="text"  id="my_text" onkeypress="valid_numbers(event);" size="30"/>
<script type="javascript">
function valid_numbers(e)
{
        var key=e.which || e.KeyCode;
        if  ( key >=48 && key <= 57)
         // to check whether pressed key is number or not 
                return true; 
         else return false;
}
</script>

What I want is:

onkeypress of my_text if the pressed key is number allow otherwise deny writing of the character.

But the above code doesn't work, my_text element accept any character, what is wrong with the above code ?!..

Help Please!..

Share Improve this question edited Aug 21, 2019 at 18:53 Brian Tompsett - 汤莱恩 5,88372 gold badges61 silver badges133 bronze badges asked Sep 7, 2016 at 6:18 Saddam MeshaalSaddam Meshaal 5524 gold badges16 silver badges33 bronze badges 5
  • there is a lot of content about how to make this, here is one example: stackoverflow.com/questions/469357/… and if you want to use jquery maybe this can help stackoverflow.com/questions/995183/… – Enmanuel Duran Commented Sep 7, 2016 at 6:22
  • Possible duplicate of Allow only numeric value in textbox using Javascript – Abhijeet Commented Sep 7, 2016 at 6:23
  • 1 onkeypress="return valid_numbers(event);" – Jaromanda X Commented Sep 7, 2016 at 6:23
  • do you want the only numbers to be input in textbox? – user6472290 Commented Sep 7, 2016 at 6:24
  • What about using <input type="number"> ? – Ruhul Commented Sep 7, 2016 at 6:27
Add a comment  | 

7 Answers 7

Reset to default 4

I think the easy way to do this would be:

<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57' />

But the problem comes up when you paste some text then HTML5's number type input may be a better choice:

<input type="number" />

BTW you can find better suggestions if you search the SO like this.

Some very complex answers, but it can be as simple as using a regular expression to return the result of checking if the input was a number or not:

<input onkeypress="return /\d/.test(String.fromCharCode(event.keyCode || event.which))">

Note that this will not stop entering of non-digit characters by pasting, dragging and dropping or script.

var input = document.getElementById('my_text');
input.onkeydown = function(e) {
    var k = e.which;

    if ( (k < 48 || k > 57) && (k < 96 || k > 105) && k!=8) {
        e.preventDefault();
        return false;
    }
};

and

<input type="text"  id="my_text" size="30"/>

Allows only [0-9] number, numpad number, arrow, BackSpace, Tab, Del as wel as Ctrl + C, Ctrl + V, Ctrl + A. Allow only one dot. Remove if last char is dot on a blur event.

element.on('keydown', function(e) {
    var arrowsKeyCodes = [37, 38, 39, 40];
    var numPadNumberKeyCodes = [96, 97, 98, 99, 100, 101, 102, 103, 104, 105];
    var dots = [110, 190];
    var tabBackDel = [8, 9, 46];
    var acv = [65, 67, 86];

    // Allow only one dot.
    if (e.target.value.indexOf('.') !== -1 && dots.indexOf(e.keyCode) !== -1) {
        event.preventDefault();
    }

    // allow only [0-9] number, numpad number, arrow,  BackSpace, Tab, Del
    // Ctrl + C, Ctrl + V, Ctrl + A
    if (
        (e.keyCode < 48 &&
            arrowsKeyCodes.indexOf(e.keyCode) === -1 || e.keyCode > 57 &&
            numPadNumberKeyCodes.indexOf(e.keyCode) === -1 &&
            dots.indexOf(e.keyCode) === -1
        ) &&
        tabBackDel.indexOf(e.keyCode) === -1 &&
        (e.ctrlKey === false || e.ctrlKey === true && acv.indexOf(e.keyCode) === -1)
    ) {
        e.preventDefault();
    }
});

element.on('blur', function(e) {
    var value = e.target.value;
    if (value.substring(value.length - 1) === '.')
        e.target.value = value.substring(0, value.length - 1)
});

Have not tried this out, but you can use parseInt in your function to check like:

var t = document.getElementById("my_test").value;

if (parseInt(t) == 'NaN') {
    // not a number
}
<html>
<body>
    <input id="edValue" type="text" onKeyPress="edValueKeyPress()" onKeyUp="edValueKeyPress()"><br>
   </body>

<script>
    function edValueKeyPress()
    {
        var edValue = document.getElementById("edValue");
        var s = edValue.value;

        var lblValue = document.getElementById("lblValue");

      if  ( s >=48 && s <= 57)

         // to check whether pressed key is number or not 
               return true; 


         else return false;



    }
</script>    

</html>

Use this tested code.it's help you.

You can Simply use JavaScript or HTML5 to execute this JavaScript:

<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input>

HTML 5 (does not require JavaScript, and also does not behave in standard way in many modern browsers.)

<input type="number">

FIDDLE

More on this may look here.

发布评论

评论列表(0)

  1. 暂无评论