HTML and CSS Reference
In-Depth Information
EXAMPLE 17.47 ( CONTINUED )
9 <form name="form1" onSubmit="return checkCC(this);">
Please select a credit card from the menu:
<p>
10 <select name="select1" size="3">
<option value="Visa">Visa</option>
<option value="MC">MasterCard</option>
<option value="Dis">Discover</option>
</select>
<p>
Please enter your card number:
</p><p>
11 <input type=textbox name="text" size=30 />
</p>
12 <input type=submit value="Check card">
<input type=reset>
</form>
</big>
</body>
</html>
EXPLANATION
1
A function called checkCC() is defined. It takes one parameter, a reference to a form.
2
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.
3
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.
4
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.
5
If the card is a Visa card, the case statements will be used to check for a valid
length.
6
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.
7
MasterCard is checked here. Its number must consist of 16 characters.
8
Discover is checked here. Its number must consist of 16 characters.
9
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.
Search WWH ::




Custom Search