473,503 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Numeric Validation

sheenattt
20 New Member
I want some help for checking numeric field and string field of a textbox thru Javascript.Ive tried these coding but its not working properly.If you have some other coding plz do fwd it.

<html>
<head>
<SCRIPT LANGUAGE="JavaScript">

function checkNumeric(objName)
{
var numberfield = objName;
if (chkNumeric(objName) == false)
{
numberfield.select();
numberfield.focus();
return false;
}
else
{
return true;
}
}

function chkNumeric(objName)
{
// only allow 0-9 be entered, plus any values passed
// (can be in any order, and don't have to be comma, period, or hyphen)
// if all numbers allow commas, periods, hyphens or whatever,
// just hard code it here and take out the passed parameters
var checkOK = "0123456789";
var checkStr = objName;
var allValid = true;
var decPoints = 0;
var allNum = "";

for (i = 0; i < checkStr.value.length; i++)
{
ch = checkStr.value.charAt(i);
for (j = 0; j < checkOK.length; j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}

alertsay = "Please enter a value greater than or "
alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
alert(alertsay);
return (false);

}
</script>

</head>
</html>
Nov 20 '06 #1
3 6748
AricC
1,892 Recognized Expert Top Contributor
So are you looking to only allow numeric values? If so:

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.  
  3. function CheckKeyCode()
  4. {
  5.   if( (event.keyCode == 189 || event.keyCode == 109) ||
  6.       (event.keyCode >= 48 && event.keyCode <= 57) || 
  7.       (event.keyCode >= 96 && event.keyCode <= 105) ) {
  8.     return true; }
  9.   else {
  10.     return false;
  11.   }
  12. }
  13. </script>
  14.  
  15. <b>Numbers: </b><input type="text" onkeydown="return CheckKeyCode()"/>
  16.  
Nov 20 '06 #2
sheenattt
20 New Member
I am pasting my page here with your code,kindly help me by correcting it whereever needed.Am working on C#.net.Actually am very new to JavaScript.Thanks

<script runat="server">

void Page_Load()
{
//Button1.Attributes.Add("onClick", "return CheckKeyCode");
}

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">

/*function CheckKeyCode()
{
if( (event.keyCode == 189 || event.keyCode == 109) ||
(event.keyCode >= 48 && event.keyCode <= 57) ||
(event.keyCode >= 96 && event.keyCode <= 105) ) {
return true; }
else {
alert("Enter numeric");
return false;
}
}*/
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" ></asp:TextBox><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
Nov 21 '06 #3
AricC
1,892 Recognized Expert Top Contributor
You using it incorrectly you need to add the following code to your text box you are trying to validate via a button click the code is the keypress event.

Add this to your text box:
Expand|Select|Wrap|Line Numbers
  1. onkeydown="return CheckKeyCode()"
  2.  
Nov 22 '06 #4

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

Similar topics

30
3637
by: Dr John Stockton | last post by:
It has appeared that ancient sources give a method for Numeric Date Validation that involves numerous tests to determine month length; versions are often posted by incomers here. That sort of code...
16
8335
by: Keith | last post by:
Am I crazy - to be shocked that there is no Numeric, Alpha, and AlphaNumeric Property in on the Textbox control I searched and searched - and found other people's code - but they don't work...
2
2906
by: Chad | last post by:
I have a textbox control, txtMeasurement, that I want to allow only numeric decimal input. I thought to use a client side validation control to ensure that the data entered is of type "Double". ...
2
9089
by: craig.wagner | last post by:
I have an element in my schema defined as follows: <xs:element name="BillingDate" type="xs:dateTime" nillable="true" minOccurs="0"/> I use the schema to validate incoming documents using an...
1
1858
by: dineshsanduke | last post by:
hi i m new user i created programatic data grid. i want numeric cell validation in data grid view
8
18170
by: .Net Sports | last post by:
I am checking for text input on a form validation in javascript that required at least one numeric character along with any number of alpha characters for a given input text box. The below is a var...
4
21500
by: Thiyagusathya | last post by:
Hi., Please help me for java script validation for text and numeric field.
3
2077
by: aris1234 | last post by:
Hi.. i have field in record using type : numeric, how to make validation and i want viewing messagebox if user inputing character or empty? this code just work for empty field, not for numeric...
16
12176
by: tshad | last post by:
I have a string field and a decimal fields and was wondering what the best way to check for numeric is? I have to do something like: If (myAmount is numeric) { total += myAmount; }
0
7087
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
7281
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
7334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7462
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...
1
5014
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...
0
3168
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...
0
1514
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 ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
383
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.