Home >> Tutorials >> Javascript tutorials
Javascript Form Validation Javascript form validation is commonly used program in webdesign. Form validation is the process of checking that a form has been filled in correctly before it is processed. For example, if your form has a box for the user to type their email address, you might want your form handler to check that they’ve filled in their address before you deal with the rest of the form.There are a number of different types of fields that you can use in forms on your web site. The types of form fields that are supported by HTML are: text fields password fields radio buttons check boxes drop down lists submit button reset button other buttons image buttons and text areas. In a web form,you may want to have certain fields mandatory. This example will be handy in such cases. The following example shows how to do text field validation and textarea validation using javascript. <!doctype html public "-//w3c//dtd html 4.0 transitional//en"> Javascript radio buttons validation Suppose you have a radio button in your form and you want the users to select at least one option. You can do this by using javascript radio button validation script.See the example below. Your Gender: Male Female Put the following piece of javascript in your function function ValidateForm(form) Try the following code: <script LANGUAGE="JavaScript"> Validating checkboxes using javascript Suppose you have a checkbox in your form and you want the users to select that before submiting the form. You can use Javascript checkbox validation script. See the example below. Do you agree to the Terms and Conditions? Yes Put the following Javascript in your function function ValidateForm(form) if ( form.terms.checked == false ) { alert ( "Please check the Terms & Conditions box." ); return false; } Try the following code:
Validating drop-down lists (select list validation) using javascript Suppose you have a drop-down lists in your form and you want the user to select the option before submiting the form. You can do this by using select list validation script.See the example below Your Age: Please Select an Option: 0-18 years 18-30 years 30-45 years 45-60 years 60+ years Put the following Javascript validation script in your function function ValidateForm(form) if ( form.age.selectedIndex == 0 ) { alert ( "Please select your Age." ); return false; } Try the following code: <script LANGUAGE="JavaScript"> Top |
Page contents: itechies.net – Learn how to do form validation,textfield/textarea validation,checkbox validation,radio button |