Introduction to Forms
On this page I give the code you need for checking user input in a form. Using these bits of code, you can write your own scripts to validate forms
I can't give you a complete script, because each form and each check are different. You'll have to use the elements I explain on this page to build your own script. I created an example form and script that you can study to get the hang of it.
On this page I discuss the limitations of using JavaScript to check a form, then I'll explain the onsubmit event handler, followed by the fewmethods and properties of the form itself. Then it's time for accessing the form elements and the specific syntax for accessing the user definedvalue of form elements.
See also Jeff Howden's excellent article Forms & JavaScript Living Together in Harmony for some of the most common usability errors and their solutions.
Limitations
First of all, you should have a clear idea of what happens when a user submits a form that has a JavaScript validation script:
- The form is checked by a JavaScript like the one described below. If the script finds a mistake the submission is halted here. The user sees an alert and is asked to re-enter some data.
- If nothing is wrong—or if JavaScript is disabled—the form is sent to the server and is processed by a CGI script.
- If the CGI script finds a mistake it generates some HTML with an error message and sends it back to the user. In this case the user has to go back to the form, re-enter some values and again submit it.
- If no mistakes are found, the CGI script does whatever it has to do with the data and directs the user to a Thank You page.
As you see, the form is checked for mistakes twice: by the JavaScript and by the CGI script. The CGI check always works, since CGI is server side. The JavaScript check only works when the user has JavaScript enabled. It follows that the CGI check is the most reliable: it always works regardless of what browser is used. Then why use a JavaScript check too?
The JavaScript check is very useful in addition to the CGI check because it can catch mistakes before the form is actually sent to the server. Thus the user doesn't have to use his back button to return to the form, something that may cause confusion, and then search for the incorrect form field, which may cause even more confusion. Therefore the JavaScript check is more user friendly than the CGI check.
In addition, when you use JavaScript the server doesn't need to spend quite so much time in error handling and is thus a little quicker. This only matters if you have lots and lots of forms, but it's good to keep it in mind.
So JavaScript is not a fail-safe method of catching mistakes, but it is very useful as an addition to CGI checks since it lightens the load on the server and is more user friendly. Therefore I recommend using bothJavaScript form checks and CGI form checks. This way, you get both user friendliness and security.
onsubmit
When using JavaScript to check a form, the first thing you need is theonsubmit
event handler. This event handler specifies a script that is executed when the user submits the form. That script is where you check whether certain fields have a value, whether the user has checked at least one checkbox, and any other checks you deem necessary.
The general syntax is:
Now you can access these elements by:
document.personal.name document.personal.address document.personal.city
The advantage of using names is that you can put all elements somewhere else in the page and still have a working script, while a script using numbers will have to be changed. After all, the input box city isdocument.forms[0].elements[2]
in the example above, but when you suddenly put it at the top of the form, it becomesdocument.forms[0].elements[0]
and you have to change the script.
Determining values
Of course, the most important thing is to find out what the user has filled in or checked in the form. At other times you might want to fill in something in the form.
Below are snippets of scripts that help you access form elements. All of them are meant to send the user input to the variable user_input
. After you've done that, you can check this value for whatever you want.
If you'd like to study a practical example, see the example form and script.
Texts, textareas and hidden fields
Very simple:
user_input = document.forms[0].text.value
where text
is the name of the text field, textarea or hidden field. Thevalue
of this element gives the text, so we transfer it to user_input
.
Writing is also possible:
document.forms[0].text.value = 'The new value';
Select boxes
Select boxes are simple too:
user_input = document.forms[0].select.value;
To change the selected option in a select box, you have to change itsselectedIndex
, like
document.forms[0].select.selectedIndex = 2;
Now the third option in the box is selected.
Old browsers
In old browsers, select boxes didn't yet have a value
property. Back then, this was the way to find the value of a select box:
var selectBox = document.forms[0].select; user_input = selectBox.options[selectBox.selectedIndex].value
First, we need to find out which option the user has selected.document.forms[0].select.selectedIndex
gives us the number of the selected option. JavaScript has created an array options
which contains all options of the select box. So we ask for the selected option in this array and take the value
of it, which we transfer to user_input
.
Checkboxes
Checkboxes need a slightly different approach. We already know their values, but want to know whether the user has checked them. Thechecked
property tells us. It can have two values: true
or false
.
Now we do:
if (document.forms[0].checkbox.checked) { user_input = document.forms[0].checkbox.name }
in which checkbox
is the name of the checkbox. If the checkbox ischecked
, we take its name
(or its value
, if you need that bit of data) and transfer it to user_input
.
To check a checkbox, set its property checked
to true
:
document.forms[0].checkbox.checked = true
Radio buttons
Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go through all radio's and see which one's checked
property is true.
where radios
is the name of the group of radio buttons.
Note that document.forms[0].radios
is an array filled with all radio buttons. Loop through all of them and see if it is checked
. If one is, transfer the value
of that radio button to user_input
.
document.forms[0].radios.length
gives the number of radio buttons. For each radio button, we see if it isTo check a radio button, set its property checked
to true
:
document.forms[0].radios[i].checked = true;
No comments:
Post a Comment