Validating Number.

vishalneeraj-24503
Posted by vishalneeraj-24503 under JavaScript category on | Points: 40 | Views : 340
function Validate_Numbers(a)
{
var numStr = "1234567890";
var thisChar;
var counter = 0;
for (var i = 0; i <= a.length-1; i++)
{
thisChar = a.substring(i, i + 1);

if (numStr.indexOf(thisChar) != -1)
{
counter++;
}
}
if (counter == (a.length))
{
alert("valid Number");
return true;
}
else
{
alert("Enter valid Number");
}
}

<asp:Button ID="Button3" runat="server" Text="Button"
OnClientClick="Validate_Numbers('vishalkumar'); return false;"/>
<asp:Button ID="Button4" runat="server" Text="Button"
OnClientClick="Validate_Numbers('84235423535'); return false;"/>


Output:- valid number
Enter valid Number

Comments or Responses

Posted by: kgovindarao523-21772 on: 12/4/2014 Level:Bronze | Status: [Member] [MVP] | Points: 10
Hi,

Why shouldn't we check whether the given text is number or not using JavaScript isNaN() Function.
function Validate_Numbers(a)
{
var isNotNumber=isNaN(a);
if(isNotNumber)
{
alert("Enter valid Number");
}else alert("Valid Number");
return isNotNumber;
}

Login to post response