Showing posts with label textbox to allow only numbers. Show all posts
Showing posts with label textbox to allow only numbers. Show all posts

28 March 2012

restrict textbox to allow only numerics

function numerics()
{
 key = String.fromCharCode(window.event.keyCode);
 if (!(key >= '0' && key <= '9'))   window.event.keyCode=0;
}
<asp:TextBox ID="txt1" Text=""  runat="server"  onkeypress=" return numerics();"></asp:TextBox>

function isAlpha(keyCode)
{
    return ((keyCode >= 65 && keyCode <= 90) || keyCode == 8||keyCode==37||keyCode==39||keyCode==9||keyCode==46)
}

function isNum(keyCode)
{
    return((keyCode < 105 && keyCode > 96)||(keyCode < 57 && keyCode > 48) || keyCode == 8||keyCode==37||keyCode==39||keyCode==9||keyCode==46)
}

<asp:TextBox ID="txtUserName" runat="server" onkeydown = "return isAlpha(event.keyCode);" onpaste =  "return false;" Width="130px"></asp:TextBox>

public static bool IsNumeric(String strVal)
    {
        Regex reg = new Regex("[^0-9-]");
        Regex reg2 = new Regex("^-[0-9]+$|^[0-9]+$");
        return (!reg.IsMatch(strVal) && reg2.IsMatch(strVal));
    }