Programming Assignment 2
Validating Forms with JavaScript
Background:
Creating forms with HTML is simple! You can use the <input>
tag in conjunction with the attributes "text" for textbox, "radio" for
round radio buttons, and "checkbox" for square checkboxes to allow
the user to enter in information. The attribute "submit" is used
to submit the form (to email or to a script for further processing)
and "reset" is used to clear the form.
Javascript functions called "methods" in object oriented programming) can
be used to check the form for errors. While
completely "bullet-proofing" the information entered by the user is
a difficult task, one can check for simple errors rather easily. The
Javascript can be executed when the submit button is clicked by means of
the <form> tag with the attribute: "onSubmit".
Specifications:
You will create a form that allows the user to enter a subscription
for an imaginary newspaper.
Click
here for a sample of what your page should look like.
Your Javascript will check:
- that the user has subscribed to at least one paper
- that the user has entered something each of the last name, first name,
and address fields
- the user has entered selected either to supply an email address or a
phone number. If the user enters email, the address should contain a '@'
and a '.', otherwise an error message is issued.
Once the form is verified, the user should get a message stating that the
subscription has been entered.
Preparation:
Don't panic! This isn't hard. You can use this web page as a model:
Click
here for a model to work from. How was this done? (Hint:
Go to the View menu and choose Page Source).
Getting Started:
-
You need to be sure you are in your public_html directory.
login to your account (if you are not logged in) and type:
>cd public_html
Then to verify that this worked type:
>pwd
Creating the Form:
- As with the last assignment, you will need to use a unix editor to
create the web page. You may use any editor, but pico is the
easiest. (It has an interface like pine, if you have used pine for
email.) Click here
for basic pico commands
- You may want to copy and paste the Javascript from form.html to
help prevent making mistakes. In the browser, when you are viewing the
Page Source, select all the code and then type Ctrl-c (copy). Then go
back to pico and paste the code into your new document.
- Important! Before testing your form, be sure to change the
email address after "mailto:" to your email address.
Hints:
Remember these programming rules:
- Functions (or "methods") start with the word "function" followed by a
function name
and parentheses that may contain a value that is being sent to the
function (an "argument").
- Functions begin with { and end with }
- conditional statements are in the format:
if (condition)
{
//statements go here
}
- Statements you want Javascript to ignore (comments) begin with //
- You will need to use the logical operators:
Troubleshooting:
If you get a Javascript error, or if it just doesn't work. Read back over
what you have written carefully. A single missing quote or typo
can prevent it from working. Remember that case matters!
For An Extra Challenge:
Using the the function that checks for a valid email address as a model,
see if you can write a function that checks for a valid phone number.
Click
here to return
Copyright © 1999-2002 All Rights
Reserved