★ wanayoo — archive 1999 http://javascript.about.com/compute/javascript/library/weekly/aa122899b.htmNouvelle recherche | Portail wanayoo
Enter Sweepstakes! · Advertise on this site · Get Bargains
About.com  
You are here:  About > Computing/Technology > Focus on JavaScript
An About.com Site
   About.com
 
 One of Over 700 Sites
Your Guide with Guide
Nate Kassebaum
Bio | Contact

Subjects
Compatibility
Reference
Animation Scripts
Cookie Scripts
Image Scripts
Text Scripts
Frame Scripts
Form Scripts
HTML Scripts
Navigation Scripts
Password Scripts
Time Scripts
Window Scripts
Software
Beginner Tutorials
Cookie Tutorials
Image Tutorials
Text Tutorials
Form Tutorials
Frame Tutorials
Multimedia
Navigation Tutors
Time Tutorials
Handling Variables
Window Tutorials

Subject Library 

All articles on this topic

Bookstore
Find books related to this topic Click Here

Videostore
Find videos related to this topic Click Here

ShoppingAbout
Your favorite products, right here Click Here

Stay up-to-date!
Subscribe to our newsletter.

Do you like our sites?
Wish to share them with others - and earn money?
Become an Affiliate

Luna Network
Apply to become
a partner
for this site.
Search for 
in   Sites A to Z
 
 

"VERIFYING NUMBERS" > Page 1, 2, 3, 4, 5

Credit Card Numbers and ZIP Codes

When verifying credit card numbers, ZIP codes and the like, usually the only things you care about are:

  • That the user enters the correct number of digits
  • That the user enters only numbers, and no other characters

In a ZIP code, for example, you would want the user to enter five characters all of which are numbers.  An example would be "90210."

Checking the length of the string is easy.

if (myString.length != 5) {return false;}

Verifying that each digit is actually a number takes a little more work, but here's the way I do it.

First, we'll set up a for loop to cycle through each of the characters.  Then, we'll check each character against each number (0-9) to see if it matches.  If we find a match every time, we're okay.  Here's what I'm saying in code:

// Cycle through each number
for (var i = 0; i < myString.length; i++)
{
    // We haven't found a match yet...
    isNumber = 0;
    // Cycle through 0-9, and set isNumber if we find one
    for (var j=0; j<10; j++) if ("" + j == myText.charAt(i)) isNumber = 1; 
}

So, the complete code for the isZIP function would look like this:

function isZIP(myNumber)
{
    if (myNumber.length != 5) {return false;}
    for (var i = 0; i < myNumber.length; i++)
    {
        isNumber = 0;
        for (var j=0; j<10; j++) if ("" + j == myNumber.charAt(i)) isNumber = 1;
        if (isNumber == 0) {return false;}
    }
    return true;
}

To check for a credit card number (Visa/MC), you would do the same thing except check for sixteen digits instead of five.

function isCC(myNumber)
{
    if (myNumber.length != 16) {return false;}
    for (var i = 0; i < myNumber.length; i++)
    {
        isNumber = 0;
        for (var j=0; j<10; j++) if ("" + j == myNumber.charAt(i)) isNumber = 1;
        if (isNumber == 0) {return false;}
    }
    return true;
}

Next Page > Social Security Numbers > Page 1, 2, 3, 4, 5

Enter an email address to send this page:
 
More Options
 
 Advertising
AllBusiness.com
Create your business plan. Here’s how.

Casino Online
Act NOW for 20% Cash Bonus $$

HotJobs.com
Better Jobs for a Better Life

ShopNow
Over 40,000 merchants in one place!

single pixel
single pixel
Marketplace
register domains
get a website$499
long distance deal
business products
0% intro apr
great dsl deal
high paying jobs
quick jobs search
findausedcar
activity planning
build a business
shop around!
$50 off mp3 player
register a domain

 
Related sites
single pixel
on About 
Focus on Java
Focus on Web3D
HTML
Perl
Personal Web Pages
Web Design
XML


Explore More On The About.com Network
Search
Arts/Humanities ·  Autos ·  Cities/Towns ·  Comedy ·  Computing/Technology ·  Cultures ·  Education ·  Food/Drink ·  Gadgets ·  Games ·  Health/Fitness ·  Hobbies ·  Home/Garden ·  Homework Help ·  Industry ·  Internet/Online ·  Jobs/Careers ·  Kids ·  Money ·  Movies ·  Music/Performing Arts  ·  News/Issues ·  Parenting/Family ·  People/Relationships ·  Pets ·  Recreation/Outdoors ·  Real Estate ·  Religion/Spirtuality ·  Science ·  Shopping ·  Small Business ·  Sports ·  Style ·  Teens ·  Travel ·  TV/Radio · 
 
single pixel

For more information, visit
About Us, Be a Guide, or Advertise. For rules of use,
read our User Agreement and
Privacy Policy.

Having a problem?
Report it here.

 © 2000 About.com, Inc. All rights reserved.