Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

There is a problem in my code.
I have developed a code using javaScript that allows only numeric value to enter in text box, but how can I define it on the on click event ?
Even we know that Text Box does not has on click Event Please help me out.

Thanks in advance
Here is the code.
JavaScript
function NumberOnly()
{
    var AsciiValue = event.keyCode;

    if ((AsciiValue >= 48 && AsciiValue <= 57) || (AsciiValue == 8 || AsciiValue == 127))
       event.returnValue = true;
    else
       event.returnValue = false;
}

HTML
<input type = "text" id = "text1"  önkeypress = "return NumberOnly()" >
Posted
v3
Comments
[no name] 24-Aug-12 13:04pm    
Do you mean the onfocus event of the textbox?
Abhimanyu vij 24-Aug-12 13:12pm    
there is no such kinDA OF EVENT IN THE TEXTBOX i think
Sandip.Nascar 24-Aug-12 13:19pm    
Who said to you, text box doesn't have any onclick event?
<input type="text" id="text1" önclick="javascript:void(0);" />
Abhimanyu vij 24-Aug-12 13:22pm    
when i am trying to write it in the textbox it does not show even it shows only ontextchanged

Here is the simplest of simplest examples I've used plenty.
HTML
<html>
<head>
   <script type="text/javascript">
   function onYourControlKeyDown(e){	
   // Arrows, backspace, tab, shift, control, end, home, delete
   if ((e.keyCode >= 37 && event.keyCode <= 40) || e.keyCode == 8 || e.keyCode == 9 ||
      e.keyCode == 16 || e.keyCode == 17 || e.keyCode == 35 || e.keyCode == 36 || e.keyCode == 46) {
      return true;
      }
   // Numbers are awesome...
   if ((e.keyCode >= 48 && e.keyCode <= 57 && e.shiftKey == false) ||
      (e.keyCode >= 96 && e.keyCode <= 105 && e.shiftKey == false)) {
      return true;
   }	
   e.preventDefault();
   return false;
   }
   </script>
</head>
<body>
   <input type='text' onkeydown='onYourControlKeyDown(window.event);' />
</body>
</html>
 
Share this answer
 
v2
Comments
__TR__ 25-Aug-12 14:14pm    
A Neat and tidy function. I wonder why was it voted down.
My 5!
Espen Harlinn 28-Aug-12 9:31am    
5'ed! Nice implementation :-D
Take a look at this jquery plugin, i've used it a few times and it works pretty good. http://www.decorplanit.com/plugin/[^]

One other thing I would do is validate the control server side as well. A user can always by pass your validation client side with firebug or some similiar debugging tool so once the page is posted back just do a double check on the control that the textbox value is actually an integer.

C#
int.TryParse(txtBox.Value, out tmpInt)


Hope that helps
 
Share this answer
 
Comments
fjdiewornncalwe 28-Aug-12 9:59am    
Agreed that validation has to take place on the server. It is never good to assume that client side validation has been successful.
You have used event.keyCode inside the javaScript, but you have not passed any event to the function.

You have to do like below.
önkeypress = "return NumberOnly(event)"

If I modify your code then it will look like below.
XML
<HTML>
   <HEAD>
   <SCRIPT language="Javascript">

      function NumberOnly(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }

   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="text1" onkeypress="return NumberOnly(event)" type="text" name="text1">
   </BODY>
</HTML>

It will work for you.

Note: Please change your naming conventions. Don't write id as text1 instead write txtNumber. And for function don't write NumberOnly instead write IsNumberKey.

Thanks...
 
Share this answer
 
v2
Comments
Abhimanyu vij 24-Aug-12 13:31pm    
its not working
Check
How Can I Use Javascript to Allow Only Numbers to Be Entered in a TextBox?
You can find the code and one textbox just above the code labelled as "Try It!", in which you can only enter numbers. Try it...
i think you don't need to use it..
<input type = "text" id = "text1"  önkeypress = "return NumberOnly()" >

instead of it i use this in Page_Load event..and it works fine..
yourTextboxName.Attributes.Add("onkeydown", "return isAlphaNumeric(event.keyCode);");
 
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