Click here to Skip to main content
Rate this: bad
good
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
Comments
Kenneth Haugland at 3-Aug-12 22:20pm
   
In what system?
Wes Aday at 3-Aug-12 22:50pm
   
Isn't item template ASP?
Kenneth Haugland at 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 at 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;
}
</script>
 
and same page load event on cs file
TetxBox1.Attributes.Add("onkeypress", "return isNumberKey(event)");
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi,
Try this:
HTML:
<asp:TextBox ID="txtUID" runat="server" CssClass="TextBox" onkeypress="return onlyNumbers(this);"/>
JAVASCRIPT:
//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.
 

 
--Amit
  Permalink  
v3
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Have a look at the RequiredFieldValidator[^].
 
This article[^] should also give you some updates.
  Permalink  

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



Advertise | Privacy | Mobile
Web01 | 2.8.150224.1 | Last Updated 4 Aug 2012
Copyright © CodeProject, 1999-2015
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