Validating Number (Javascript)

Self-Innovator
Posted by Self-Innovator under JavaScript category on | Points: 40 | Views : 1210
Page Design

<table align="center">
<tr>
<td>
Age
</td>
<td>
<asp:TextBox ID="txtAge" Onchange="return ValidateNo(this)" runat="server"></asp:TextBox>
</td>
</tr>
</table>


Javascript Function
function ValidateNo(x) {
var len = x.value.length;
var s_charcode = 0;
for (var s_i = 0; s_i < len; s_i++) {
s_charcode = x.value.charCodeAt(s_i);
if (!((s_charcode >= 48 && s_charcode <= 57))) {
alert("Enter only numbers");
x.value = "";
x.focus();
return false;
}
}
return true;
}

Comments or Responses

Posted by: Rimi1289 on: 2/9/2013 Level:Starter | Status: [Member] | Points: 10
You can also try this Script

    function isNumberKey(evt) {

var charCode = (evt.which) ? evt.which : event.keyCode
if (event.which != 45 && (event.which != 46 || $(this).val().indexOf('.') != -1) && (event.which < 48 || event.which > 57))
return false;

return true;
}


The above script accepts "Only Numbers" with a "DOT".

Rimi

Login to post response