jkkenzie Posted April 23, 2008 Share Posted April 23, 2008 Hi! I would like to ensure that only integers 0 - 100 are keyed in. I have not tried it before, any idea? Quote Link to comment Share on other sites More sharing options...
RichardRotterdam Posted April 24, 2008 Share Posted April 24, 2008 I think this wheel probably has been invented like a million times. search and you shall find a script that already does this http://www.cambiaresearch.com/c4/029c978b-aac5-472e-97a8-95b256f5febd/How-Can-I-Use-Javascript-to-Allow-Only-Numbers-to-Be-Entered-in-a-TextBox.aspx Quote Link to comment Share on other sites More sharing options...
jkkenzie Posted April 28, 2008 Author Share Posted April 28, 2008 Thanks: Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted April 28, 2008 Share Posted April 28, 2008 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; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.