How to restrict user to type only numeric values in textbox

March 26th, 2009 No comments

<!– just copy and paste it to your text editor like notepad and save file as html –>

<HTML>
<HEAD>
<SCRIPT language=”javascript”>
function _allowNumeric(e)
{
var keyp;
keyp = getKeyCode(e);

if(keyp >= 48 && keyp <= 57)
{
return true;
}
else if(keyp == null)
{
return true;
}
else
{
if(keyp == 8 || keyp == 0)
{
return true;
}
else
{
return false;
}
}
};

function getKeyCode(e)
{
if (window.event)
{
return window.event.keyCode;
}
else if (e)
{
return e.which;
}
else
{
return null;
}
};

</SCRIPT>
</HEAD>
<BODY>
Type your phone number : <INPUT TYPE=”text” onKeyPress=”javascript: return _allowNumeric(event);” onpaste=”return false;” />
</BODY>
</HTML>
<!– hope this help you –>

Categories: JavaScript Tags: