0

I want my textbox in HTML page only accept number from 0 to 10, also accept: 0.5, 1.2, ... Thanks alot

  • You're probably going to need to use javascript. Here's an SA post which deals with the same issue that may help you out: http://stackoverflow.com/questions/463299/how-do-i-make-a-textbox-that-only-accepts-numbers – Billy Moat Nov 22 '12 at 11:17

3 Answers3

0

You can use Regex for doing that!

Dreaam
  • 25
  • 7
0

Try this

<script>
function checkfloat(e,field){
     if (!(((e.keyCode>=48)&&(e.keyCode<=57))||(e.keyCode==46)))
     {
        e.keyCode=0;                   
     }     
 if (e.keyCode==46)
 {
    var patt1=new RegExp("\\.");        
    var ch =patt1.exec(field);       
    if(ch==".")
    {
        e.keyCode=0;
    }                        
 }
} 
</script>
<input type="text" onkeypress="checkfloat(event, this.value)">
Renato Lochetti
  • 4,558
  • 3
  • 32
  • 49
0

The HTML5 number input type can do this:

<input type="number" min="0" max="10" />

However browser support for this is still poor. Only Chrome, Safari and Opera support this at this point in time.

Give it a test in Chrome though.

Demo

3dgoo
  • 15,716
  • 6
  • 46
  • 58