Click here to Skip to main content
12,943,528 members (52,598 online)
Rate this:
Please Sign up or sign in to vote.
See more:
In my web application i have morethan 50 textboxes in a single page. Only numeric values are allowed to type inside these texboxes. If a letter is typed then it must show a error message. Right now i did this by using javascript function and calling it in every text box "onkeypress" . Is there any other method to do this. I Dont want to add onkeypress attribute in every textbox. I places all my textboxes in a panel.

<script type="text/javascript">
        function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode;
            if ((charCode >= 48 && charCode <= 57) || charCode == 08 || charCode == 43 || charCode == 45 || charCode == 127) {
                return true;
            else {
                alert('Please Enter Numbers Only');
                //return false;
                 evt.preventDefault ? evt.preventDefault() : evt.returnValue = false;

<asp:TextBox ID="txtnumber" runat="server" MaxLength="6"  onkeypress="return isNumberKey(event)" >
Posted 19-Feb-13 22:51pm
Updated 20-Feb-13 20:58pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can take help from jquery object and bind event handler like as follows

DOM ready event handler
     isNumberKey(event);//call your javascript function

My suggestion is you assign a commin css class to each textbox you want to validate and use this css class from jquery method like as follows

     isNumberKey(event);//call your javascript function
shamjid 20-Feb-13 7:30am
@Ahasan: First of all thanks for ur response. I tried ur code,its working but a small problem. If i tried to type a letter it shows the alert message but it also insert the letter in the texbox. If i type "asd" whenever i typed "a" it shows the alert message ,but it insert "a" in the textbox..
S. M. Ahasan Habib 20-Feb-13 11:11am
instead of return false you should use
event.preventDefault ? event.preventDefault() : event.returnValue = false;

I already update your method IsNumericKey now it will work. Please Re run your code.
shamjid 21-Feb-13 0:57am
@Ahasan:The problem is with firefox only..It works fine in chrome and IE
S. M. Ahasan Habib 21-Feb-13 1:32am
So if you want to solve it with all browsers then you use this statement instead of just return false.
event.preventDefault ? event.preventDefault() : event.returnValue = false;
shamjid 21-Feb-13 1:46am
@Ahasan: I aready used ur statement..but still not working with Firefox
S. M. Ahasan Habib 21-Feb-13 2:59am
yes one typeo is there evt will be there instead of event. I update your question. Please read it again and execute it firebox now and see the result. I tested it in firefox and found that it is working.
shamjid 21-Feb-13 3:49am
@Ahasan:-Its working fine..Thanks a lot..5
shamjid 21-Feb-13 6:28am
@Ahasan:-please go through the below link.This is my another ?

I hope u ll get me some solution..Thank u
S. M. Ahasan Habib 21-Feb-13 9:27am
I provided solution for that problem too. Please visit that link which you mention above and make sure it will work for you.
shamjid 25-Feb-13 6:38am
@Ahasan:-Thanks bhai...u solved my two question..ur codes works fine..i need other help from you..please check below link
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Have a look here[^] It might be the cleanest way to do things.

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
OriginalGriff 4,643
CHill60 2,970
Maciej Los 2,348
Jochen Arndt 1,900
ppolymorphe 1,765

Advertise | Privacy | Mobile
Web01 | 2.8.170518.1 | Last Updated 21 Feb 2013
Copyright © CodeProject, 1999-2017
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