Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

XML
<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;
            }

        }

</script>


XML
<asp:TextBox ID="txtnumber" runat="server" MaxLength="6"  onkeypress="return isNumberKey(event)" >
Posted
Updated 20-Feb-13 20:58pm
v3

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

DOM ready event handler
JavaScript
$(function(){
  $('input').keypress(function(event){
     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

JavaScript
$(".mytext").keypress(function(event){
     isNumberKey(event);//call your javascript function
  });
 
Share this answer
 
Comments
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
Have a look here[^] It might be the cleanest way to do things.
 
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