Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 29-Jan-13 2:37am
v2

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

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

JAVASCRIPT:
//Restrict the user to key-in chrectors and other special charectors
JavaScript
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.
 
Share this answer
 
Comments
[no name] 29-Jan-13 8:54am    
+5
Thanks @Sisir.
[no name] 30-Jan-13 2:16am    
yw
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
Member 12370938 5-Mar-16 1:52am    
thanks ...for above code
Declare textbox within ur grid view like this

C#
<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
C#
<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 :)
 
Share this answer
 
v2
Comments
Shruthi.BT 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...
[no name] 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 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.. ?
[no name] 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
fjdiewornncalwe 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)



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