Allow only Numeric Value in TextBox using JavaScript

Rajesh_Kumar
Posted by Rajesh_Kumar under JavaScript category on | Points: 40 | Views : 1386
<script type="text/javascript">

var specialKeys = new Array();
specialKeys.push(8); //Backspace

function Allow_Numeric(e)
{
var keyCode = e.which ? e.which : e.keyCode
var ret = ((keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
document.getElementById("error").style.display = ret ? "none" : "inline";
return ret;
}
</script>

<input type="text" id="text1" onkeypress="return Allow_Numeric(event);"/>
<span id="error" style="color: Red; display: none">* Input digits (0 - 9)</span>

Comments or Responses

Login to post response