First we described which character that allow to enter in TextBox (_allowChar). We put the e.keyCode which is represent the keystroke in a variable _keycode, then we put the unescape of keystroke in a variable char, then we will find the keystroke character in a variable _allowChar using indexOf functionality. We check is it the character exist or not. indexOf function will find the proper character from the string variable which is called this function, if doesn’t exist then indexOf will return -1 (integer) and if there is exist then indexOf will return 0. You can see the whole function below this.
function NumericValidation(e)
{
var _allowChar = “0123456789″ + unescape(‘%’ + (8).toString(16));
var _keycode = e.keyCode;
var char = unescape(‘%’ + _keycode.toString(16));
return (_allowChar.indexOf(char)!=-1);
}
This is the sample of how we use NumericValidation function. This function will call in OnKeyPress event in a TextBox.
<HTML>
<HEAD>
<title>Numeric</title>
<script language=”javascript”>
function NumericValidation(e)
{
var _allowChar = “0123456789″ + unescape(‘%’ + (8).toString(16));
var _keycode = e.keyCode;
var char = unescape(‘%’ + _keycode.toString(16));
return (_allowChar.indexOf(char)!=-1);
}
</script>
</HEAD>
<body>
<form id=”Form1″ method=”post”>
<input type=”text” name=”Text1″ onkeypress=”NumericValidation(event)”>
</form>
</body>
</HTML>