How Do I Enter Only Numbers In A Form?

free web hosting
Open Discussion > CONTRIBUTE > Computers > Programming Languages > Java, Java Servlets, Java Script, & JSP

How Do I Enter Only Numbers In A Form?

kvarnerexpress
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

Reply

cse-icons
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...

CODE
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/articl...ic.asp?ID=ROLLA

Hope it helped u...

Cheers.


Reply

Mike
^ 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:

CODE


<?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;
}

?>

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.


*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for enter, numbers, form

*MORE FROM TRAP17.COM*
advertisement



How Do I Enter Only Numbers In A Form?



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE