You should have read chapters 2-6 for today. None of the CONCEPTS in these chapters are new. Only the IMPLEMENTATION in this new language might be unfamiliar to you.
Lab for today: A Purchase FormFirst, write a form to take a user's name, mailing address address, shipping address, phone number, email address, credit card number and expiration date.
Validate each of these fields according to rules that make sense to you from a business standpoint. What fields are required? What fields are optional? What are the proper data types for each field? Can you restrict the phone number to being just numbers? Can you restrict the zip code field to being just 5 digits and only numeric? What is the best way to do this?
You can use onblur() to validate each field as they fill it in, or you can create a "submit" button that will "submit" the form to a javascript function using onclick() or onsubmit(). (Instead of submitting the form data to a php script, just send it to the javascript function(s) for now. You can print the values in an alert box if you like.)
--STOP HERE AND SHOW ME WHAT YOU'VE DONE--
If you are up for a challenge, do the following:
1. Validate the email address format.
2. Validate the phone number format.
and the most difficult:
3. Implement a
credit card number validation routine in Javascript. Here is another description of the Luhn checker:
"For a card with an even number of digits, double every odd numbered digit and subtract 9 if the product is greater than 9. Add up all the even digits as well as the doubled-odd digits, and the result must be a multiple of 10 or it's not a valid card. If the card has an odd number of digits, perform the same addition doubling the even numbered digits instead."
Prompt the user to enter a card number. You can do this in a variety of ways (have them click a button to launch a prompt() box, etc).
Once you have the number, pass this number to a function that performs a series of checks to make sure that this is a valid credit card number. If it is not a valid number, return an error. If it is a valid number, tell the user "thanks".
Here are the checks you will perform:
1. Is this a number? If yes, keep going. If not, return error.
2. Get the length of the entire number - use number.length()
3. Starting at the right, take
every other digit and double it. So skip the rightmost digit, and double the next one, and so on.
4. If the result is greater than 10, then add the two digits together.
5. Now add up all the digits. If the sum is divisible by 10, then the number is valid, return success. If not, then the number is not valid, return error.
Hint 1: suppose you have the credit card number stored in a var called "ccnumber". You can use a for loop that increments a counter called "i" and loop through the digits with ccnumber.charAt(i). charAt() will give you a specific digit in the number.
Hint 2: Use the modulus operator, which in Javascript is %. In the algorithm above, step 3 asks you to do something to "every other" digit, starting at the right. How can you determine what every "other digit" is?
First: take the length of the number and divide by 2, take the modulus. The "mod" will either be 0 or 1. (If the number is even, the modulus will be 0, if the number is odd, the modulus will be 1.) Store this mod in a variable.
Next: now that you know whether the number is odd or even, you can determine how to do "every other" digit. Start looping through every digit of the number. (You can start at the left, it's ok. You can use a charAt() and a for loop as described in Hint 1 or on page 192 in your book. Start with a counter set to 0 in your loop.)
Finally: Divide the counter (i) you are looking at by 2 and take the modulus. If the counter you are looking at has a modulus that matches the mod you stored above, then the digit you are looking at is an "every other one" and should be doubled according to the procedure above. If the modulus is NOT matching, this means that you should NOT double this digit.