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? Link to comment https://forums.phpfreaks.com/topic/102466-allow-interger-inputs-only-on-an-input-box/ 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 Link to comment https://forums.phpfreaks.com/topic/102466-allow-interger-inputs-only-on-an-input-box/#findComment-526305 Share on other sites More sharing options...
jkkenzie Posted April 28, 2008 Author Share Posted April 28, 2008 Thanks: Link to comment https://forums.phpfreaks.com/topic/102466-allow-interger-inputs-only-on-an-input-box/#findComment-528729 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; } } Link to comment https://forums.phpfreaks.com/topic/102466-allow-interger-inputs-only-on-an-input-box/#findComment-528868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.