HTML and CSS Reference
EXAMPLE 17.47 ( CONTINUED )
9 <form name="form1" onSubmit="return checkCC(this);">
Please select a credit card from the menu:
10 <select name="select1" size="3">
Please enter your card number:
11 <input type=textbox name="text" size=30 />
12 <input type=submit value="Check card">
A function called checkCC() is defined. It takes one parameter, a reference to a form.
If the value of selectedIndex is 0 , the first option in the select list was chosen, a Visa
card. The rest of the statements in this if block check to see which card was select-
ed if it wasn't this one.
The variable cc_length is assigned the number of characters that were typed into
the text box; that is, the number of characters in the credit card number.
The switch statement will be used to check for valid card number lengths for
whichever card the user selected from the select menu. The variable cc_type con-
tains the card type: Visa, MasterCard, or Discover.
If the card is a Visa card, the case statements will be used to check for a valid
The valid length for the Visa credit card number is between 13 and 16 characters
(as shown in Figure 17.51). If the card number length is between these numbers,
true is returned.
MasterCard is checked here. Its number must consist of 16 characters.
Discover is checked here. Its number must consist of 16 characters.
The form starts here. The onSubmit handler will be triggered when the user clicks
the submit button. At that time the credit card will be checked for a valid number
of characters in the number provided by the user. This is not a complete check.
You can combine the other functions from this section to provide a more thorough
check; we haven't checked here to see if the value entered is numeric, has strange
characters, or empty fields.