I'm looking for a javascript that only allows to write numbers into my text field,

BR
BR used Ask the Experts™
on
I'm looking for a javascript that only allows to write numbers into my text field,
if anybody try to write a letter the javascript should warn the user.
please write a number not letter,
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Try this one that I found on the Internet:
<HTML>
   <HEAD>
   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(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="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </BODY>
</HTML>

Open in new window

BRDigital Marketing

Author

Commented:
It's a very good function,
however, I need if somebody writes a letter, it should give a warning (alert) that the user should write numbers not letters.
can you help me out pls?
Sure
  <HEAD>
   <SCRIPT language=Javascript>
      <!--
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            alert('Only numbers are allowed.');
            return false;
         }
 
         return true;
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">
   </BODY>
</HTML>

Open in new window

Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

BRDigital Marketing

Author

Commented:
You are perfect :)
Top Expert 2007

Commented:
>>however, I need if somebody writes a letter, it should give a warning

How very annoying - if a single mis-typed letter creates a popup and requires a click to dismiss, you are going to work your Poor Users into a fine fettle.
BRDigital Marketing

Author

Commented:
Dear Badotz,
you are right actually,
what do you recomend?
should I open a new question for your comment?
Top Expert 2007

Commented:
Not necessary.

One wonders why only numbers are allowed...and wouldn't it be better to wait for the user to finish typing before you check for compliance? I mean, you could strip out the non-numerics *afterwards* and THEN alert the Poor User why you changed her input.
Try this one as a bonus :D. That way it won't interfere with user activity.

Cheers,
Tien
  <HEAD>
   <SCRIPT language=Javascript>
      <!--
      var warning = null;
      function isNumberKey(evt)
      {
         if (!warning) warning = document.getElementById('warning');
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57)) {
            warning.innerHTML = 'Only numbers are allowed.';
            return false;
         }
         warning.innerHTML = '';
         return true;
      }
      //-->
   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar"><span style="color:red" id="warning"></span>
   </BODY>
</HTML>

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial