numberic/alphabetic keypress in JS

Java Script Sample

Here are 2 Java Script functions on how to filter numeric / alphabetic characters typed in an edit box:

Screen numeric inputs

function onKeyPressBlockNumbers(e)
{
var key = window.event ? e.keyCode : e.which;
var keychar = String.fromCharCode(key);
reg = /d/;
return !reg.test(keychar);
}

Screen numeric inputs

function onKeyPressBlokcLetters((eventObj)
{
var keycode;

if(eventObj.keyCode) //For IE
keycode = eventObj.keyCode;
else if(eventObj.Which)
keycode = eventObj.Which;  // For FireFox
else
keycode = eventObj.charCode; // Other Browser

if (keycode!=8) //if the key is the backspace key
{
if (keycode<48||keycode>57) //if not a number
return false; // disable key press
else
return true; // enable key press
}
}

HTML Sample



Numeric Validation

Name :

Phone:

You may also like