Numeric Field Validation using Javascript

Hi guys,

During development sometimes we may need to validate some input fields for numeric field. Below is the java script function that works with almost all browsers.

Use this code in your code and pass KeyPress() event as an argument to this function.

function NumericValidation(eventObj)
    var keycode;
    if(eventObj.keyCode)//For IE
        keycode = eventObj.keyCode;
    else if(eventObj.Which)
        keycode = eventObj.Which;  // For FireFox
        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
            return true; // enable key press



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s