user3247075
user3247075

Reputation: 293

NUMBERS ONLY: TextBox in GridView without validators

How do I make a textbox in gridview accept numbers only without using validators? I am using C#, ASP.net.

I'm hoping to use KeyPress event or something.

Upvotes: 1

Views: 4812

Answers (1)

Aditya Singh
Aditya Singh

Reputation: 9612

<asp:TextBox ID="txtUID" runat="server" CssClass="TextBox" onkeypress="return onlyNumbers(this);"/>

//Restrict the user to key-in chrectors and other special charectors
function onlyNumbers(evt) {
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

Reference:- CodeProject

Upvotes: 3

Related Questions