Validate the Text Box value is a Numeric Or Not Using Java Script

Chikul
Posted by Chikul under JavaScript category on | Views : 3187
Call the Function "fnValidate" on "onblur" event of the textBox.

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function fnValidate(obj)
{
if (isNumeric(obj.value)==false)
{
obj.value= "0.00"
}
}


function isNumeric(strNum)
{
var isNumber=true;
var VarDec = 0;
var validChars = "0123456789.-";
var thisChar;
for (i = 0; i < strNum.length && isNumber == true; i++)
{
thisChar = strNum.charAt(i);
if (thisChar == "-" && i > 0) isNumber = false;
if (thisChar == ".")
{
VarDec = VarDec + 1;
if ((i==0 || i == strNum.length-1) && strNum.length ==1) isNumber = false;
if (VarDec > 1) isNumber = false;
}
if (validChars.indexOf(thisChar) == -1) isNumber = false;
}
return isNumber;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server" onblur="fnValidate(this)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" /></div>
</form>
</body>
</html>

Comments or Responses

Login to post response