Monday, October 29, 2012

Textbox accept digits

script language="javascript" type="text/javascript">
        function CheckNumeric(e) {

            if (window.event) // IE
            {
                if ((e.keyCode < 48 || e.keyCode > 57) & e.keyCode != 8) {
                    event.returnValue = false;
                    return false;

                }
            }
            else { // Fire Fox
                if ((e.which < 48 || e.which > 57) & e.which != 8) {
                    e.preventDefault();
                    return false;

                }
            }
        }
    
function IsNumeric(sText)
{
    var ValidChars = "0123456789.";

    //loop through the string passed in, iif its in the validchars list, continue, otherwise, return false
    for(i = 0; i < sText.length; i++) 
    { 
        if(ValidChars.indexOf(sText.charAt(i)) == -1) 
        {
            return false;
        }
    }

    return true;
}

function validateNumerical(evt)
{
    var theEvent = evt || window.event;
    var key = theEvent.keyCode || theEvent.which;
    key = String.fromCharCode( key );
    var regex = /[0-9,\.]/;
    if( !regex.test(key) )
    {
        theEvent.returnValue = false;
        if(theEvent.preventDefault)
        {
            theEvent.preventDefault();
        }
    }
}

    </script>
   
    <input type="text" onKeyPress="CheckNumeric(event)" />

No comments: