Click here to Skip to main content
13,556,419 members
Rate this:
Please Sign up or sign in to vote.
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 2:35am
Updated 29-Jan-13 2:37am
Rate this: bad
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[^].

<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.

You can add your logic for restricting the number ranges in function onlyNumbers(evt).

taditdash 30-Jan-13 1:54am
Thanks @Sisir.
taditdash 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...

User-12337438 5-Mar-16 1:52am
thanks ...for above code
taditdash 6-Mar-16 6:14am
Most welcome buddy :)
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Declare textbox within ur grid view like this

<asp:textbox id="txtUID" runat="server" cssclass="TextBox" onkeypress="return isNumberKey(event);" xmlns:asp="#unknown" />

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;


hope this helps u

enjoy :)
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...
kshitij2410131 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.. ?
kshitij2410131 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
algioethqnbalkerunb 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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02-2016 | 2.8.180515.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2018
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