Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Gridview validation
hi.... i am using a grid view and inside the grid view a text box... but that text box should restrict user from entering special characters or alphabets.. it should accept only and only numbers ranging between say 1 to 100 it can also be like 2.5 or 20.5 ... so what is the best way to validate the text box... inside the grid view is it better to use a validator like regular expression or is there any other way... as in grid view there will be many rows.. so whats the best way to validate the text box within grid view please help or guide me...
 

Thanking you in advance.
Posted 29-Jan-13 3:35am
Edited 29-Jan-13 3:37am
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Please follow the solution in How to validate textbox inside gridview to accept numeric values only[^].

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.

You can add your logic for restricting the number ranges in function onlyNumbers(evt).
 
Thanks,
Tadit.
Comments
Tadit Dash at 30-Jan-13 1:54am
   
Thanks @Sisir.
Tadit Dash at 30-Jan-13 5:45am
   
Hi @Shruthi.BT,

Please accept this answer, if it has helped you in any way.
This will help others to find the answer in one go and you will also be awarded with some points for this action...
 
Thanks,
Tadit
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Declare textbox within ur grid view like this
 
<itemtemplate>
<asp:textbox id="txtUID" runat="server" cssclass="TextBox" onkeypress="return isNumberKey(event);" xmlns:asp="#unknown" />
</itemtemplate>
 
and in head section of the page add script
<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>
 

 
hope this helps u
 
enjoy Smile | :)
  Permalink  
v2
Comments
Shruthi.BT at 29-Jan-13 9:19am
   
hi... kshitij_CodeMaster this worked... but it is allowing any number means like 147852369 or anything in numbers... is there any way i can restrict number to like from 1- 10 as this textbox is for rating score.. so it should be within specific range... thanks...
kshitij_CodeMaster at 30-Jan-13 2:25am
   
for this task ..u cn use dropdownlist for rating bcs ur values are fixed from 1 -10.
it will be easily understandable to u.
Shruthi.BT at 30-Jan-13 8:11am
   
but... i need a text box.. means it is not 1-10 only that for ex i told.. but it should not take numbers like 123456789 also.. can we restrict that.. ?
kshitij_CodeMaster at 30-Jan-13 8:16am
   
<asp:textbox id="txtUID" runat="server" cssclass="TextBox" onkeypress="return isNumberKey(event);" MaxLength=2/>
 

set MaxLength field to no of digits u want to restrict in textbox
Marcus Kramer at 30-Jan-13 10:03am
   
Plagiarism: Source

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

  Print Answers RSS
0 OriginalGriff 6,803
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,421
3 Manas Bhardwaj 4,841
4 Maciej Los 4,330


Advertise | Privacy | Mobile
Web02 | 2.8.1411023.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
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