Jump to content
xisto Community
Sign in to follow this  
kvarnerexpress

How Do I Enter Only Numbers In A Form?

Recommended Posts

i have a form with a few fields such as zip and phone. i only want the user to type in those fields numbers and not letters, and if the user enters letters, then the form will warn the user somehow to only enter number.is there a function that will accomplish this or how is something like this accomplished?what code would i have to use?any help would be greatly appreciated.thanks__________________Kvarnerexpress

Share this post


Link to post
Share on other sites

Hi kvarnerexpress,

The following javascript function can be used on each key press to verify if it is a digit. else you can show an alert box indicating only digits are allowed. You can extend this function to incorporate more features as u like, this is just to give u a start...

function isNumeric(n){	     if((n > 47) && (n <  58))      {               return true;        }           return false;}

btw a more useful javascript function that u can use after customizing is available at:

http://www.shiningstar.net/articles/articlic.asp?ID=ROLLA

Hope it helped u...

Cheers.

Share this post


Link to post
Share on other sites

^ That's a nice script right there! You could also use PHP to make sure it is all numbers, but I'm not really sure how... It would probably be something like this though:

<?php$zipcode = $_POST['zipcode'];if(!preg_match([0-9],$zipcode)){echo '<b>Your zip code is only numbers!</b>';exit;}if(strlen($zipcode) > 6 || strlen($zipcode) < 6){echo '<b>Your zip code is 6 characters long!</b>';exit;}?>

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.