
| <SCRIPT> function validEmail(email) {if (email == "") return false; // cannot be empty // invalidChars -
invalid characters for an email address. badChar saves the position of the invalid character in the invalidChars string. For example, if the invalid character was a colon, badChar would contain 2, the position of the colon in the five-character string. String.charAt() gets the nth character from a string for (i = 0; i < invalidChars.length;
i++) { indexOf looks for the position of a character in a string. If the result of the indexOf function is 1, the character isn't in the string, and you again get a false result. String.indexOf(substring, start) returns the position of the first occurrence of the substring that appears after the start position, if any, or 1 if no such occurrence is found. substring "substring" that is to be searched for within string start an optional integer argument that specifies the position within the string at which the search is to start. Legal values are 0 (the position of the first character in the string) to string.length 1 (the position of the last of character in the string). If the argument is omitted, the search begins at the first character of the string. if (email.indexOf(badChar, 0) > -1)
return false; Checking to see if there is at least one @ within the string // there must be one
"@" symbol // atPos will only equal -1 if
there wasn't an @ within the string If an @ is found within the string, we want to make sure that there isn't more than one @, so we start searching from that position + 1, where the first @ was found, to the end of the string searching for another @. If another @ is found the indexOf will not return a 1, but will return a number, the position of the found @. If the indexOf != -1, means that an additional @ was found, hence false. // and only one "@"
symbol Checking to see if there is at least one "." within the string // periodPos will only equal -1
if there wasn't an "." within the string // and at least one "."
after the "@" email.length is a property of email that gives us the length of the email address. After the first "." is found we to make sure that two or three characters exist (.uk, .de, .com, .org, etc ) // there must be 2
or 3 characters after the "." return true; Checking: function isNum(passedVal) { // Is this a number? String.charAt() get the nth character from a string, in this case passedVal.charAt(i) is the ith digit which is then checked to see if it is a number. passedVal.length is the length or number of digits of the Number passed. charAt checks the character at the position i. If the character is less than "0" or greater than "9", it isn't a digit, so bail out and declare the input to be non-numeric, or false. If the result is true, you've a got a numeric Zip Code. for (i = 0; i < passedVal.length; i++) { if (passedVal.charAt(i) > "9")
return false; return true; Checking: function validZip(inZip) { // Is this a valid Zip Code? The value is passed to the isNum function above. // Check if the Zip Code is
numeric return false; function submitIt(form) { // make sure they enter a color form.color.selectedIndex is the (index) position of the option selected. In this particular case, the option selectedIndex = 0 has no value by design. If an individual made no selection, the condition below would exist. If an individual chose an option, by actually selecting something, then the selectedIndex would be > 0 and the remaining options all have values assigned to them, again by design. if (form.color.options[colorChoice].value == "") { return false; } Make sure they enter in a number of doors. doorOption = -1 is obviously not a valid number of car doors. doorOption is acting as a switch, only getting set if a radio button is checked. If a radio button is checked then doorOption is set to the index of that radio button, hence not equal to 1. var doorOption = -1; form.DoorCt.length is the number of radio buttons. for (i = 0; i < form.DoorCt.length; i++) { if doorOption switch is not set then we know that no radio button was checked. if (doorOption == -1) { With your forms, you'll often find that if the user makes one choice, that choice will dictate the value of other fields on the form. For example, let's say that the sunroof option is only available on a two-door model. You could deal with it this in two ways. First, you could check the entry and put up an alert dialog if the user makes the wrong choice. // can't have the sunroof with a four doorif (form.DoorCt[doorOption].value == "fourDoor" && form.sunroof.checked) { alert("The sunroof is only available on the two door model"); return false; } But it's better design to simply make the entry for the user. So if the user picks the sunroof, the script automatically clicks the two-door button. This function actually occurs at very end of the script. It is placed here for illustrative purposes only. if (sunroofField.checked) sunroofField is checked then we need to make sure that the "Two Door" radio is also checked. document.myForm.DoorCt.length is the length or number of radio buttons. We need to cycle through the entire "length", looking at the radio buttons until we find the one equal to "twoDoor", document.myForm.DoorCt[i].value == "twoDoor". Once found, we make sure that we check that particular radio button, document.myForm.DoorCt[i].checked = true function doorSet(sunroofField) {
Input. select() selects the text displayed in a text, textarea, password, or fileupload element. This method produces the same result as the user dragging the mouse across all the text in the specified text object. On most platforms, this produces the following effects:if (!validEmail(form.emailAddr.value)) { alert("Invalid email address"); form.emailAddr.focus(); form.emailAddr.select(); return false; } selectedIndex - specifies the index of the selected option within the Select element. If the Select element has it MULTIPLE attritube set and allows multiple selections, this property only specifies the index of the first selected item or -1 if none are selected. if (form.zip.value == ""
&& form.dealerList.selectedIndex == 0) { form.zip.focus(); return false; } if (!validZip(form.zip.value)) { // If we made it to here,
everything's valid, so return true function doorSet(sunroofField) { <SCRIPT> <BODY> <FORM
Your Email Address: <INPUT TYPE="text" NAME="emailAddr"> Colors: <SELECT NAME="color"> Options: <INPUT TYPE="checkbox" NAME="sunroof" VALUE="yes" Doors: <INPUT TYPE="radio" VALUE="twoDoor" NAME="DoorCt">Two Either enter your Zip code, or pick the dealer nearest you: Zip: <SELECT NAME="dealerList"> <INPUT TYPE="Submit" VALUE="Submit"> <INPUT TYPE="Reset" VALUE="Reset"> </FORM> |