This tutorial will show you how to create a JavaScript-enabled form that checks whether a user has filled in the form correctly before it's sent to the server. This is called form validation. First we'll explain why form validation is a useful thing, and then build up a simple example form, explaining things as we go along. At the end, there's a little exercise to keep you busy too!What is form validation?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 two main methods for validating forms: server-side (using CGI scripts, ASP, etc), and client-side (usually done using JavaScript). Server-side validation is more secure but often more tricky to code, whereas client-side (JavaScript) validation is easier to do and quicker too (the browser doesn't have to connect to the server to validate the form, so the user finds out instantly if they've missed out that required field!).Client-side validationClient-side form validation (usually with JavaScript embedded in the Web page)Server-side validationServer-side form validation (usually performed by a CGI or ASP script)In this tutorial we'll build a simple form with client-side JavaScript validation. You can then adapt this form to your own requirements.A simple form with validationLet's build a simple form with a validation script. The form will include one text field called "Your Name", and a submit button. Our validation script will ensure that the user enters their name before the form is sent to the server.Open this page to see it in action. Try pressing the Send Details button without filling anything in the "Your Name" field.You might like to open the source code for this form in a separate window, so that you can refer to it throughout the tutorial.You can see that the page consists of a JavaScript function called validate_form() that performs the form validation, followed by the form itself. Let's look at the form first.The formThe first part of the form is the form tag:
Now let's take a look at the JavaScript form validation function that does the actual work of checking our form.The validate_form() functionThe form validation function, validate_form(), is embedded in the head element near the top of the page:The first line (That's all there is to simple JavaScript form validation! Our example is very simple as it only checks one field. Let's expand this example with a more complex function that checks lots of form fields. We'll also look at how to check other types of fields, such as checkboxes, radio buttons and drop-down lists.A more complex formLet's look at a more complex validated form with some different types of form fields.Open this page to see it in action. Try pressing the Send Details button without filling in the form and see what happens.Again, you might like to open the source code for this form in a separate window, so that you can refer to it as we talk you through.Like our previous example, this page has a form called contact_form and a function called validate_form(). In addition to the previous text field, the form has radio buttons, a drop-down list and a checkbox.The validate_form() function now has 3 extra checks, one for each of our new fields.Validating radio buttonsAfter the contact_name text box has been checked, the gender radio buttons are validated: if ( ( document.contact_form.gender[0].checked == false ) && ( document.contact_form.gender[1].checked == false ) ) { alert ( "Please choose your Gender: Male or Female" ); valid = false; }This code checks to see whether either of the radio buttons ("Male" or "Female") have been clicked. If neither have been clicked (checked == false), the user is alerted and valid is set to false.Validating drop-down listsNext the "Age" drop-down list is checked to see if the user has selected an option. In the form, we named the first option in the drop-down list "Please Select an Option". Our JavaScript can then check which option was selected when the user submitted the form. If the first option is selected, we know the user has not selected a "real" option and can alert them: if ( document.contact_form.age.selectedIndex == 0 ) { alert ( "Please select your Age." ); valid = false; }Note that the values for selectedIndex start at zero (for the first option).Validating checkboxesFinally, the "Terms and Conditions" checkbox is validated. We want to the user to agree to our imaginary Terms and Conditions before they send the form, so we'll check to make sure they've clicked the checkbox: if ( document.contact_form.terms.checked == false ) { alert ( "Please check the Terms & Conditions box." ); valid = false; }Because we set our valid variable to false in any one of the above cases, if one or more of our checks fail, the form will not be sent to the server. If the user has not completed more than one field, then they will see an alert box appear for each field that is missing.Now you know how to write a form validation script that can handle multiple form fields, including text boxes, radio buttons, drop-down lists and check boxes!One point to note about JavaScript validation is that it can always be circumvented by the user disabling JavaScript in their browser, so for secure validation you'll need to write your validating code in your server-side scripts. However, for day-to-day use JavaScript is a quick and easy way to check over your forms before they're sent to your server.An exercise: "one field at a time" validationOur example script works by validating all the form fields at once. This can be a bit confusing for the user, especially if they've missed out more than one field, as they will get lots of alert boxes appearing and they might forget which fields they need to fill in!As an exercise, try modifying the script to only prompt the user one field at a time. For example, if they miss out the "Name" and "Gender" fields and press "Send Details", it will only prompt them for the "Name" field initially. Then, after they fill in the "Name" field and press "Send Details" again, it will prompt them for the "Gender" field.As a finishing touch, try making the script move the cursor to the field that needs filling in each time (Hint: use the focus() method to do this).Good luck!
4 comments:
cheap uggs 904089 ugg on sale 228969 uggs for sale 407876
buy tramadol without prescriptions tramadol overdose alcohol - buy tramadol online visa
parajumpers outlet s*_e& [url=http://www.jenterom.com/parajumpers.asp]parajumpers jakke salg[/url] 0m`p0 duvetica ace C5J0B canada goose jakke 40F+i canada goose vest cwVY-
buy yellow valium valium effects 10 mg - buy valium online no prescription uk
Post a Comment