473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 3572
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

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

Similar topics

4
by: Vijay Kumar R Zanvar | last post by:
Greetings, Are the following inferences of mine correct? 1. #include <string.h> char *strcpy(char * restrict s1, const char * restrict s2); a. s1 != s2 b. That means,
7
by: tweak | last post by:
Can someone give me a short example as how to best use this keyword in your code? This is my understanding: by definition restrict sounds like it is suppose to restrict access to memory...
12
by: Me | last post by:
I'm trying to wrap my head around the wording but from what I think the standard says: 1. it's impossible to swap a restrict pointer with another pointer, i.e. int a = 1, b = 2; int *...
21
by: Niu Xiao | last post by:
I see a lot of use in function declarations, such as size_t fread(void* restrict ptr, size_t size, size_t nobj, FILE* restrict fp); but what does the keyword 'restrict' mean? there is no...
2
by: noosaj | last post by:
Hi, I'm designing a basic sales tax calculator. I'm in a bind here. I am trying to figure out how to prevent the user from inputting anything other than numbers in the textboxes. Obviously,...
5
by: Dakrat | last post by:
Allow me to preface this post by saying that this is my first database project, and while I have learned a lot, any concepts I have learned are hit and miss as I have found new requirements and...
1
by: Peirski | last post by:
Hi all Situation: a form containing 10 textboxes with a button to validate those textboxes The user will have to complete these textboxes with numbers from 1 to 10 and each number can only...
3
by: sasimca007 | last post by:
Hello friends, We have seen many textboxes, it is used as date,readonly,text etc... If we want to restrict the textbox with not to enter letters only numbers and one more...
23
by: raashid bhatt | last post by:
what is restrict keyword used for? eg int *restrict p;
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.