Claim your Biolink Click Here
1 like 0 dislike
394 views

I have an html input box, but only numeric data I need. How can I do that?

in Education & Reference by (1.1k points) | 394 views

3 Answers

0 like 0 dislike

You will need an events to validate if the entered text is a number or not. See sample code

JS

·  function isValidChar(char){

·   

·      var txt = char;

·      var found = false;

·      var validChars = "0123456789"; //List of valid characters

·   

·      for(j=0;j<txt.length;j++){ //Will look through the value of text

·          var c = txt.charAt(j);

·          found = false;

·          for(x=0;x<validChars.length;x++){

·              if(c==validChars.charAt(x)){

·                  found=true;

·                  break;

·              }

·          }

·          if(!found){

·              //If invalid character is found remove it and return the valid character(s).

·              document.getElementById('txtFld').value = char.substring(0, char.length -1);

·              break;

·          }

·      }

·  }

HTML

<input type="text" id="txtFld" onKeyup="isValidChar(this.value);" />
by
0 like 0 dislike
the other way out is 

<input type="text" id="txtFld" onblur="if(!Number(this.value))
{alert('not a number');}" />

 

by
0 like 0 dislike

You can use HTML5 to validate the type of your input without using JavaScript. However, this method is not (yet) supported by all browsers.

<input type="number" name="quantity" min="1" max="5">

Use the following attributes to specify restrictions (as described here):

  • max - specifies the maximum value allowed
  • min - specifies the minimum value allowed
  • step - specifies the legal number intervals
  • value - Specifies the default value
by

Related questions

2 like 0 dislike
1 answer
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked Jan 22, 2016 in Education & Reference by Krish (1.1k points) | 402 views
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer
1 like 0 dislike
0 answers
0 like 0 dislike
1 answer
0 like 0 dislike
1 answer

Where your donation goes
Technology: We will utilize your donation for development, server maintenance and bandwidth management, etc for our site.

Employee and Projects: We have only 15 employees. They are involved in a wide sort of project works. Your valuable donation will definitely boost their work efficiency.

How can I earn points?
Awarded a Best Answer 10 points
Answer questions 10 points
Asking Question -20 points

1,310 questions
1,471 answers
569 comments
4,809 users