-8

I have a text box that I want a person to add a number, and only a number into. How would I make a function that, if the value added to the text box was not a number, they would get an alert that said, "must add a number", or something similar. without using RegEx!

thanks!

Rassisland
  • 169
  • 1
  • 3
  • 16

2 Answers2

4

http://www.w3schools.com/tags/att_input_type.asp

You want to use type="number" on your input field

Type a number:
<input type="number"/>
neaumusic
  • 10,027
  • 9
  • 55
  • 83
2

Here is a solution that you can use to only allow the user to enter numbers, they can´t add anything that´s not a number

<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>
Rashwan L
  • 38,237
  • 7
  • 103
  • 107