473,614 Members | 2,377 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

JavaScript to check whether textbox contains only integer or numeric/number values

103 New Member
Hi all,

I would like to get the Javascript code to check the textbox contains only numeric values or numbers..

I am using asp.net and c#.net..

Please help me..

Regards,
Mathew
Oct 18 '07 #1
23 138258
gits
5,390 Recognized Expert Moderator Expert
hi ...

you may use javascript built in isNaN() method for that ... it returns true in case it is NotaNumber else you get a false ...

kind regards
Oct 18 '07 #2
mathewgk80
103 New Member
hi ...

you may use javascript built in isNaN() method for that ... it returns true in case it is NotaNumber else you get a false ...

kind regards
Hi Gits,

Can you please give an example for that???

regards,
Mathew
Oct 18 '07 #3
gits
5,390 Recognized Expert Moderator Expert
ok ... lets assume following example:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2.     <head>
  3.     <script type="text/javascript">
  4.         function check_field(id) {
  5.             var field = document.getElementById(id);
  6.  
  7.             if (isNaN(field.value)) {
  8.                 alert('not a number');
  9.             }
  10.         }
  11.     </script>
  12.     </head>
  13.     <body>
  14.     <form>
  15.         <input type="text" id="t_field"/>
  16.         <input type="button" value="check" onclick="check_field('t_field');"/>
  17.     </form>
  18.     </body>
  19. </html>
  20.  
kind regards
Oct 18 '07 #4
ananth
75 New Member
HI ,
The code above using NAN will not work properly try this.

Expand|Select|Wrap|Line Numbers
  1. function checkNum(x)
  2. {
  3.  
  4.   var s_len=x.value.length ;
  5.   var s_charcode = 0;
  6.     for (var s_i=0;s_i<s_len;s_i++)
  7.     {
  8.      s_charcode = x.value.charCodeAt(s_i);
  9.      if(!((s_charcode>=48 && s_charcode<=57)))
  10.       {
  11.          alert("Only Numeric Values Allowed");
  12.           x.value='';
  13.          x.focus();
  14.         return false;
  15.       }
  16.     }
  17.     return true;
  18. }
  19.  
  20.   <input type="text" id="t_field" onChange='checkNum(this)'/>
This will execute on the field exit or tabbing out of the field.

Hope this would be useful.
Oct 19 '07 #5
gits
5,390 Recognized Expert Moderator Expert
HI ,
The code above using NAN will not work properly try this.
nope ... it is working properly for the requirement. it checks for numeric values and it works! you didn't say why or what is not working properly!

your code should check for only Integers? you simply could use one line for that:

Expand|Select|Wrap|Line Numbers
  1. // in case value is an integer test_result will be true 
  2. // otherwise it will be false
  3. var test_result = /^\d+$/.test(value);
  4.  
kind regards
Oct 19 '07 #6
mathewgk80
103 New Member
Hi all,

I would like to check whether the textbox contains only nmumberic values by using javascript...

Please help me...

Regards,
Mathew
Nov 8 '07 #7
iam_clint
1,208 Recognized Expert Top Contributor
I would use something similar to this and just call this function everytime you need to know if its numeric
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function isNumeric(val) {
  3.     var numeric = true;
  4.     var chars = "0123456789.-,+";
  5.     var len = val.length;
  6.     var char = "";
  7.     for (i=0; i<len; i++) { char = val.charAt(i); if (chars.indexOf(char)==-1) { numeric = false; } }
  8.     return numeric;
  9. }
  10. </script>
  11.  
so I would say if (isNumeric("wha tever value here")) { alert("Its Numeric!!!"); }
Nov 8 '07 #8
acoder
16,027 Recognized Expert Moderator MVP
Or use regular expressions:
Expand|Select|Wrap|Line Numbers
  1. function isNumeric(str) {
  2.  return /^\d$/.match(str);
  3. }
Nov 9 '07 #9
iam_clint
1,208 Recognized Expert Top Contributor
Acoders way is better. Never even thought to do that :)
Nov 9 '07 #10

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

Similar topics

5
13847
by: ief | last post by:
hi all, i'm trying to check the length of a numeric value in a string. this is what i need to do: I have a string "Mystring (253)" and a string "SecondString (31548745754)" Now i have to check if the string contains more than 3 numeric characters because i must only process the ones with more than 3.
2
2707
by: Reddy | last post by:
Hi, How to check whether a given number is an integer Thanks, Reddy
6
7682
by: Jan | last post by:
Hi, Is there any elegant way in VB.NET to check whether a certain string is part of a "collection" of strings. Something like IF "teststring" in {"string1", "string2", "string3", "teststring"} THEN Thanks, Jan
5
11872
by: Eli | last post by:
Hi, I want to check whether a value is a scalar. A scalar can be: - None (null) - string - number (integer, float) - boolean How can I validate a value is one of these types? I care about the value only, and not its class methods.
13
14965
by: nishit.gupta | last post by:
Is their any fuction available in C++ that can determine that a string contains a numeric value. The value cabn be in hex, int, float. i.e. "1256" , "123.566" , "0xffff" Thnx
19
107923
Frinavale
by: Frinavale | last post by:
Filtering user input is extremely important for web programming. If input is left unfiltered users can input malicious code that can cripple your website. This article will explain how to make sure that the user only submits a number to your .NET web application and also demonstrate how to add JavaScript to your ASPX pages. Upon popular demand, I have added a section that also covers how to use a validator control to check if a text box...
7
6826
by: nussu | last post by:
Hi, Plz provide me javascript : javascript for a textbox to accept the values in decimals and within range i need to enter a value in textbox like 1.03 and it should be <3 (lessthan 3). Plz help me. Rgds,
9
4146
by: bizt | last post by:
Hi, I am using the following function to validate a forms value as an integer function isNumeric(str){ var numericExpression = /^+$/; if(str.match(numericExpression)){ return true; }else{
3
7077
by: beary | last post by:
Hi, I have a number of text boxes for the user to input either numbers, or letters. Sometimes, the entry must be numeric. So I was thinking of using an onblur thing to check whether the value of the input is numeric, and if not, show an alert box to the user. I assume I would need to use getelementbyid. And on this page, there might be up to 30 similar input text fields, all of which need to be checked when the focus is lost. But...
0
8120
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8620
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8265
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8423
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6085
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5537
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4048
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4115
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2560
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.