By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Numeric Validation

sheenattt
P: 20
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
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 1,892
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
P: 20
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

Expert 100+
P: 1,892
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

Post your reply

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