1

How to make HTML input tag only accept numbers?

  • I need to make sure that input field only takes numbers as input value.

  • I want the user to be unable to type in any other characters other than numbers.

  • User manually enters the number not a number scroll bar.

I am using the Meteor Js.And the code as shown below.

App.html

<head>
  <title App</title>
</head>

<body>
  {{> main}}

</body>

<template name="main">
     <h1>Welcome to TICTACTOE App</h1>
    <p><b>Layout Number :</b> <input type="text" id="no"></p>
</template>



App.js


if (Meteor.isClient)
 {


  Template.main.events
  ({
    'keydown input#no' : function ()
    {
      // template data, if any, is available in 'this'
      if (event.which == 13) 
        { // 13 is the enter key event
          console.log("You pressed  enter key");
        }
    }
  });
}

if (Meteor.isServer) 
{
  Meteor.startup(function () 
  {
    // code to run on server at startup
  });
}
Venkat
  • 841
  • 15
  • 31
  • possible duplicate of [How to allow only numeric (0-9) in HTML inputbox using jQuery?](http://stackoverflow.com/questions/995183/how-to-allow-only-numeric-0-9-in-html-inputbox-using-jquery) – CRABOLO Jan 07 '14 at 05:09
  • And possible duplicate of [HTML Text Input allow only Numeric input](http://stackoverflow.com/questions/469357/html-text-input-allow-only-numeric-input) – ELavicount Jan 07 '14 at 05:18

7 Answers7

2

This should help you.

<input type="number" />

:)

Or Best way to use jQuery with keycodes:

jQuery("#yourInputId").keydown(function(event) {
            // Allow: backspace, delete, tab, escape, enter and .
            if ( jQuery.inArray(event.keyCode,[46,8,9,27,13,190]) !== -1 ||
                 // Allow: Ctrl+A
                (event.keyCode == 65 && event.ctrlKey === true) || 
                 // Allow: home, end, left, right
                (event.keyCode >= 35 && event.keyCode <= 39)) {
                     // let it happen, don't do anything
                     return;
            }
            else {
                // Ensure that it is a number and stop the keypress
                if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                    event.preventDefault(); 
                }   
            }
        });

See DEMO.

Shreejibawa
  • 1,860
  • 1
  • 25
  • 35
1

if you use HTML5 then

<input type="number" />

else

<script>
     function isNumberKey(evt){
            var charCode = (evt.which) ? evt.which : event.keyCode
            return !(charCode > 31 && (charCode < 48 || charCode > 57));
        }
</script>

<input type="text" onkeypress="return isNumberKey(event);">
Cris
  • 12,799
  • 5
  • 35
  • 50
1

With HTML5 it's easy:

<input type="number" />

If HTML5 is not supported use jQuery events: Live Demo

<label for="number">Number: </label>
<input type="text" name="number" id="number" />

<script>
    $('#number').keydown(function(){
        $(this).val($(this).val().replace(/[^\d]/,''));
        $(this).keyup(function(){
            $(this).val($(this).val().replace(/[^\d]/,''));
        });
    });
</script>
rullof
  • 7,124
  • 6
  • 27
  • 36
1
function isNumberKey(evt){
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

<input type="someid" name="number" onkeypress="return isNumberKey(event)"/>
0

will work only with HTML 5. If you want it to work for older browser as well you have to take help of javascript.

Ashok
  • 487
  • 5
  • 22
0

Try this

<input name="amount" type="text" value="Only number in here"/> 

<script>
    $('input[name=amount]').keyup(function(){
        $(this).val($(this).val().replace(/[^\d]/,''));
    });
</script>

You can also use the pattern attribute in html5:

<input type="text" name="name" pattern="[0-9]" title="Title" /> 
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
0

On any input you need filter, you only need put the onkeydown="return onlynumbers(event);" code. This is a generic code that will work on old HTML and does not need jQuery. :)

<script type="text/javascript"> 
function onlynumbers(e) {
        if (e.shiftKey === true ) {
            if (e.which == 9) {
                return true;
            }
            return false;
        }
        if (e.which > 57) {
            return false;
        }
        if (e.which==32) {
            return false;
        }
        return true;
    }
</script>
<input type="text" onkeydown="return onlynumbers(event);">
Renan Gomes
  • 71
  • 1
  • 4