Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to validate textbox inside gridview item templates to accept numeric values only
Posted
Comments
Kenneth Haugland 3-Aug-12 22:20pm    
In what system?
[no name] 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.
Leo Rajendra Dhakal 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)");

Hi,
Try this:
HTML:
ASP.NET
<asp:TextBox ID="txtUID" runat="server" CssClass="TextBox" onkeypress="return onlyNumbers(this);"/>

JAVASCRIPT:
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
 
Share this answer
 
v3
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.
 
Share this answer
 
Have a look at the RequiredFieldValidator[^].

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900