Monday 17 October 2011

Restrict user to enter only number using javascript

Code:-



<script type="text/javascript">
//Function to allow only numbers to textbox
function validate(key)
{
//getting key code of pressed key
var keycode = (key.which) ? key.which : key.keyCode;
var phn = document.getElementById('txtPhn');
//comparing pressed keycodes
if ((keycode < 48 || keycode > 57))
{
return false;
}
else
{
//Condition to check textbox contains ten numbers or not
if (phn.value.length <10)
{
return true;
}
else
{
return false;
}
}
}
</script>

<asp:TextBox ID="txtPhn" runat="server" onkeypress="return validate(event)"></asp:TextBox>

No comments:

Post a Comment