Date Added: January 23, 2008 03:33:53 AMUsing JavaScript with HTML FormsOne of the commonest problems that web masters come up against when web programming with forms is that users often neglect to enter the correct information. Client side HTML form validation with JavaScript solves this problem by intercepting the standard browser submit function with a JavaScript submit equivalent. The JavaScript example in this article illustrates the technique, and shows how to manipulate the JavaScript document model, create a JavaScript popup that alerts the user to the problem, and organize JavaScript code properly in a user defined function. The theory is simple, for those who understand JavaScript already:
So, the code is in two parts : defined in the HTML document head, and called from the body, through the form's submit button. The JavaScript Validation FunctionHere is the skeleton for the validation function contained in the head of the HTML document: function form_handler()
{ return true; } The validation process is very simple - for each error found in the HTML form, the function should return false. If it gets to the end without finding an error, it returns true to indicate that the form is valid. The JavaScript document model allows us to access individual form elements; the field is contained within the form, which is contained within the document. The fields and forms can be referenced by name. In the examples below, it is assumed that the following form has been constructed: <form name="test_form" method="post" action="/action.php" onsubmit="return form_handler();">
<input type="text;" name="text_field"> <input type="submit;" value="Submit"> </form> The important points in the above HTML snippet are:
Assuming that the validation tests to see whether the text_field is empty, the following JavaScript code can be added to the validation function: if ( document.test_form.text_field == "" )
{ alert ("Please fill in the text field!"); return false; // Prevent form submit } In the above, a JavaScript popup is used to alert the user, using the JavaScript alert function, if the text_field is empty. Next, the function returns false, to prevent the form from being submitted. Validating Other Fields using JavaScript ValidationAll fields can be validated using the above technique. It is up to the programmer to determine what dependencies there might be between the fields, and validate them accordingly. It is good practice to avoid multiple alerts in the same validation process, which is why the above code returns a value before completing all validations. Checkboxes can be validated by testing their checked property against true or false: if ( test_form.checkbox_name.checked == false ) // Unchecked
Drop-down listboxes can be checked in two ways - using the selectedIndex property to detect for non-selection of items, or testing to see what item is actually selected: if ( test_form.listbox_name.selectedIndex == 0 ) // Nothing selected
if ( test_form.listbox_name.options[test_form.listbox_name.selectedIndex].value == "" ) // Empty selection? Finally, radio buttons can be tested to see if there is one selected, by evaluating each item in the collection using the array of options linked to the set of radio buttons: if ( test_form.radio_name[0].checked == false ) // Is the first one selected?
if ( test_form.radio_name[1].checked == false ) // Is the second one selected? etc. Of course, in this last example, the programmer must be slightly careful to be sure that the correct sense is maintained - false should only be returned if none of the radio buttons are checked. | SearchPopular Categories |