HTML and CSS Reference
In-Depth Information
<form name="myForm"
onSubmit="return validExpire(this)">
Enter the month (02 or 2):
<input name="expireMonth" type="text" size=5 />
Enter the year (2003 or 03):
<input name="expireYear" type="text" size=5 />
<input type="submit" value="submit" />
<input type="reset" value="clear" />
A function called validExpire() is defined. It takes one parameter, a reference to a
form. Its purpose is to validate the expiration date of a credit card.
A new Date object is created and assigned to the variable called now .
Using the getMonth() method, we get this month (months start at zero) and add 1.
Using the getFullYear() method, we get the current year, as 2003 .
The parseInt() function converts the expiration year, as typed in by the user, to an
integer. Then we get the length of the year, and convert the month into an integer.
If the number of characters in the year, yearLength , is 2, then 2000 is added to the
expireYear value. If the user typed 02, then the new value is 2002.
If the value of expireMonth is less than the value of thisMonth and the value of ex-
pireYear is equal to the value of thisyear, or the value of expireMonth is greater than
12, the number entered is invalid. So a card is invalid if it has a month prior to
this month and the card expires this year, or the month is over 12, because there
are only 12 months in a year.
If the expiration year is prior to this year, it is also invalid.
The form starts here.
The onSubmit event handler is triggered after the user fills out the form and clicks
the submit button. When he or she does, the function called validExpire() is
called. It will return true if the expiration date is valid, and the form will be sent
to the URL assigned to the action attribute of the form. See Figure 17.50.
Search WWH ::

Custom Search