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
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
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
gits 5,390
Recognized Expert Moderator Expert
ok ... lets assume following example: - <html>
-
<head>
-
<script type="text/javascript">
-
function check_field(id) {
-
var field = document.getElementById(id);
-
-
if (isNaN(field.value)) {
-
alert('not a number');
-
}
-
}
-
</script>
-
</head>
-
<body>
-
<form>
-
<input type="text" id="t_field"/>
-
<input type="button" value="check" onclick="check_field('t_field');"/>
-
</form>
-
</body>
-
</html>
-
kind regards
HI ,
The code above using NAN will not work properly try this. - function checkNum(x)
-
{
-
-
var s_len=x.value.length ;
-
var s_charcode = 0;
-
for (var s_i=0;s_i<s_len;s_i++)
-
{
-
s_charcode = x.value.charCodeAt(s_i);
-
if(!((s_charcode>=48 && s_charcode<=57)))
-
{
-
alert("Only Numeric Values Allowed");
-
x.value='';
-
x.focus();
-
return false;
-
}
-
}
-
return true;
-
}
-
-
<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.
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: - // in case value is an integer test_result will be true
-
// otherwise it will be false
-
var test_result = /^\d+$/.test(value);
-
kind regards
Hi all,
I would like to check whether the textbox contains only nmumberic values by using javascript...
Please help me...
Regards,
Mathew
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 -
<script>
-
function isNumeric(val) {
-
var numeric = true;
-
var chars = "0123456789.-,+";
-
var len = val.length;
-
var char = "";
-
for (i=0; i<len; i++) { char = val.charAt(i); if (chars.indexOf(char)==-1) { numeric = false; } }
-
return numeric;
-
}
-
</script>
-
so I would say if (isNumeric("wha tever value here")) { alert("Its Numeric!!!"); }
acoder 16,027
Recognized Expert Moderator MVP
Or use regular expressions: - function isNumeric(str) {
-
return /^\d$/.match(str);
-
}
iam_clint 1,208
Recognized Expert Top Contributor
Acoders way is better. Never even thought to do that :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
by: Reddy |
last post by:
Hi,
How to check whether a given number is an integer
Thanks,
Reddy
|
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
|
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.
|
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
| |
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...
|
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,
|
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{
|
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...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
| |