Jump to content

Allow Interger inputs only on an input box


jkkenzie

Recommended Posts

Why use character codes when you can use regex?  Comments are for the OP.

 

function validateNumber(){
   var num = document.getElementById('input').value; // <-- 'input' is a generic name...use the actual form input id
   var regex = /^[0-9]{3}$/; // <-- this says "3 characters that are in the class 0 thru 9"

   if(!num.match(regex)){ // <-- if the number DOES NOT match the pattern, give an error
      alert(num + " isn't a 3 digit number!");
      return false;
   }
   else{
      return true;
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.