![]() |
![]() |
Labs
|
|---|
JavaScript provides us with a variety of objects and arrays that can be used to enhance our scripting efforts. We already make use of a couple of objects in our order form script. In particular, the document object is used to produce our confirmation page and the Math object is used in function convertToMoney. Arrays are used in our getSelection and getRadio functions to manipulate groups of form elements. We can extend the page further still using the Date and String objects, and some homegrown arrays, as follows.
Edit function "submitForm" now so that, instead of printing out the entire credit card number on the confirmation page, it prints out something like: XXXX-XXXX-XXXX-1234. In this case, X's are used except for the last four digits entered, which appear as entered on the order form. String method "subString" and String property "length" will come in handy in your coding.
Cleaning up the presentation of the year is easy - just add 1900 to the value returned by Date's getYear method. Fixing the month is a bit trickier. To do so, you can create an array of Strings, corresponding to the names of the months. Then, you can use the value returned by Date's getMonth method as an index into your array to retrieve the name of the current month.
Here is another approach to this problem. You could create an array of the quantity values (six numbers), and then use a for-each loop to iterate through the array, checking each number one at a time. As before, if any of the values is invalid, an alert box appears and processing is discontinued. Make this last change to function "submitForm" now.
|