Click here to Skip to main content
12,750,710 members (38,413 online)
Rate this:
Please Sign up or sign in to vote.
How to validate textbox inside gridview item templates to accept numeric values only
Posted 3-Aug-12 17:17pm
Kenneth Haugland 3-Aug-12 22:20pm
In what system?
Wes Aday 3-Aug-12 22:50pm
Isn't item template ASP?
Kenneth Haugland 3-Aug-12 22:52pm
Coould be Silverlight/WPF as well, but my guess would be ASP based on what he has asked previously.
creatives.system 4-Aug-12 1:09am
of course, yes, Its asp item template inside gridview. i've successfully applied outside gridview but unable to apply inside gridview. ASP.NET 4.0

I've successfully applied outside gridview with following methods
in aspx page<title>ExtendedGridView demo</title>
<script type="text/javascript">
function isNumberKey(evt)
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57) )
return false;
return true;

and same page load event on cs file
TetxBox1.Attributes.Add("onkeypress", "return isNumberKey(event)");
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Try this:
<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;

And also use RequiredFieldValidator if the field is required to key-in by user.

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Assuming this is not a web app, because if it was, you'd have said so, handle the keypress or keydown event for your textbox, and call a method that checks Char.IsDigit and Char.IsControl and set Handled = true so that the keypress is rejected. If this is a web app question, tag your questions properly.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Have a look at the RequiredFieldValidator[^].

This article[^] should also give you some updates.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170215.1 | Last Updated 4 Aug 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100