Thursday, 23 June 2011

Allow only correct numeric value in a text box Thourgh HTML and JavaScript


function numeric(e,txtObj)
{
    var RetVal = true;

    e = (e) ? e : window.event;
    var charCode = (e.which) ? e.which : e.keyCode;

    // ? , End, Home, Left, Right
    if (charCode == 8 || charCode == 35 || charCode == 36 || charCode == 37 || charCode == 39)
    {
        RetVal = true;
    }
    else if (charCode == 46)
    {
        if((txtObj.value).indexOf('.') == -1)       
            RetVal = true;
        else
            RetVal = false;
    }
    else if (charCode > 31 && (charCode < 48 || charCode > 57))
    {       
        RetVal = false;
    }
   
    return RetVal;
}

//Calling from HTML
<input type='text' maxlength='15' onKeyPress="JavaScript:return numeric(event,this);"></input>

No comments:

Post a Comment