469,343 Members | 5,693 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,343 developers. It's quick & easy.

How do you restrict textboxes to only numbers?

For a textbox I want the user to only enter in a number from 1 to 10(including numbers like 4.5 and 6.6 and so forth). How would I do this?
Mar 30 '08 #1
5 3371
eWish
971 Expert 512MB
You will need a Server-side script or Programing language for this. HTML Can limit the number of characters entered by using the maxlength attribute. But it can not distinguish the difference between a number or letter.

--Kevin
Mar 30 '08 #2
Using javascript, here is a start:

[HTML]<input type='text' name='num' onkeyup="this.value=this.value.replace(/[^0-9\.]/g, '');" />[/HTML]

I tried code to stop the user entering more than 1 decimal place, or more than 10, but it made it too aggressive.

Would you like me to write more code, to restrict to: X or 10 or X.X, and append just a warning message after the box ?

Regards,
Anthony
Mar 30 '08 #3
eWish
971 Expert 512MB
Moving to JavaScript Forum
Mar 30 '08 #4
mrhoo
428 256MB
var Rx= /^(10|([1-9](\.\d+)?))$/;

This expression matches either '10',
or a string whose first character(^) must be an integer greater than zero [1-9].
This first digit may be followed by a decimal point (\.) followed by 1 or more digits between 0 and 9 (\d+).
The $ flags the end of the string, prohibiting any invalid characters following a match.

For example-
Expand|Select|Wrap|Line Numbers
  1. function validator(field,rx){
  2.     var val= field.value;        
  3.     if(rx.test(val)) return val;
  4.     field.value= '';
  5.     throw '(Invalid input in '+field.name+')\n'+field.title;    
  6. }
// fake an input for this test
var inputfield= {name:'oneten',value:'',title:'Type a number between 1 and 10'};
var Rx= /^(10|([1-9](\.\d+)?))$/;
inputfield.value='10.12';

alert(validator(inputfield,Rx))
Mar 30 '08 #5
malav123
217 100+
HI,
If you want the user to allow for only numeric input than you can use following function.....
Expand|Select|Wrap|Line Numbers
  1. function onlyDigit(e)
  2. {    
  3.     var unicode=e.charCode? e.charCode : e.keyCode            
  4.     if (unicode!=8 && unicode!=9 && unicode!=46 && unicode!=39 && unicode!=37)
  5.     { 
  6.         if((unicode<48||unicode>57) && (unicode<96||unicode>105))
  7.         return false
  8.     }                                
  9. }
Mar 31 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by Vijay Kumar R Zanvar | last post: by
7 posts views Thread by tweak | last post: by
12 posts views Thread by Me | last post: by
21 posts views Thread by Niu Xiao | last post: by
2 posts views Thread by noosaj | last post: by
23 posts views Thread by raashid bhatt | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.