JavaScript Conditionals, More...

If you have some extra time, try the following exercise:

Exercise 3. Email Validation

Prompt the user for their email address. Then check that the email address the user entered is a valid one. Specifically, make sure that the entered string contains the symbol "@" and a "." (a dot), and that its length is sufficient to include a three-character domain after the dot.

You will need to use the Javascript built-in method indexOf(), on the String object, as well as the length property on the same object.

The indexOf() method is used to locate the position of a character in a text string. For example, if a variable called myComputer has the value "Macintosh", then myComputer.indexOf("M") would yield the value 0, since the M is the first character in the string, and we are counting from zero.

Similarly, myComputer.indexOf("i") would yield the value 3, since i is the 4th character in the string.

Also, length will give you the length of the text string. Use it as follows:

myComputer.length

and expect it to return the value 9, as the string "Macintosh" is 9 characters long.

Here is a good on-line reference to Javascript, if you'd like to reference the documentation on the String Object for this exercise.

Here is a demo of the page.

Have fun with JavaScript!


Introduction | Syllabus | Assignments | Documentation | Project