At the end, there's a little exercise to keep you busy too! 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.
You can then adapt this form to your own requirements. Let'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.
The form uses the post method to send the data off to a dummy CGI script on ELATED. In reality, you would of course send the data to your own CGI script, ASP page, etc. The return allows us to return the value true or false from our function to the browser, where true means "carry on and send the form to the server", and false means "don't send the form".
If one of our checks fails, we'll set valid to false so that the form won't be sent. If the field is empty, the user is warned with an alert box, and the variable valid is set to false.
Next, we return the value of our valid variable to the onSubmit event handler described above. If the value is true then the form will be sent to the server; if it's false then the form will not be sent:.
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. 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. In addition to the previous text field, the form has radio buttons, a drop-down list and a checkbox. This code checks to see whether either of the radio buttons "Male" or "Female" have been clicked.
Finally, 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:. Because we set our valid variable to false in any one of the above cases, best way to get gold dragon in dragonvale one or more of our checks fail, the form will not be sent to the server.
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 free ninjatrader forex feed field and press "Send Details" again, it will prompt them for the foreign exchange market structure pdf field.
Form Validation - radio Buttons Alert
As a finishing touch, try making the script move the cursor to the field that needs filling in each time Hint: Get tips, tricks and site updates once a month! Want to add a comment, or ask a question about this article?
Got a question about making a website? Ask it in the forums — we'd love to help you. Never made a website before? Read How to Make a Website. Unauthorised copying or redistribution prohibited.
My issue is how to make that text field required, but only required based on drop down selection. I know there are ways to do this with radio buttons and checkboxes, but can it be done with dropdowns as well?
The form is large so I didn't want to paste all here, but the snap shot of the items I'm referring to are: Thanks, David [Edited by catron on Feb Yes, you could write something like: Please post your question in a new topic: Hi, and thanks for all of the examples.
I am writing a script that validates two select menus. Could you please give me an example of how to check them one at a time and how to concatonate the errors if they exist? Hi Thanks for your codes. I am trying to validate "surname" in a form.
I need to check that it is not "empty" and if not they are alpha characters with - space and ' special characters and with a limit of 30 chars.
The name of my form is "myForm" I have not been able to find code anywhere that incorporates the 2 That is, empty or alpha. The name of the function is validateForm.
Something like this should do what you need I haven't tested it mind! I recently stumbled across http: I hope this will help some beginners. How would you stop just spaces being entered.
Hi Thanks for the information! I get my form to work with the validation, but everyone who first submits the form gets this error: I cant figure that out.
I'm guessing you're using cgiemail to process your form on the server? Thanks for the reply - It is a cgi script: It was only after deleting my browser history that a correction in my template. All fine thanks for the good article. Hi, If I have this code: Rather than using alert s for each field, just concatenate the error messages together into a string separated by e.
I have a problem with my form. It has three fields which I want to validate but in testing it the first incident of validation causes the alert to pop up but then when I click OK on the alert it submits the form anyway without letting me complete the field and continue to the other fields. It can be found at; http: Hi Matt, I find your site very helpful. Need help for js validation in my school project. Name, Class, CheckboxBook1, CheckboxBook2 Example Data: Each class can only select not more than 2 of Book1 and 2 of Book2 Please give me the code.
Thank you for the great code!
Current forum topics Extra Spaces on Page directories and paths Contact Form not working Checkboxes and 'submit' button issues PHP Anonymous Functions: What Are They, and Why Use Them?