There are two main methods for validating forms: (usually done using Java Script).Server-side validation is more secure but often more tricky to code, whereas client-side (Java Script) 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! In this tutorial we'll build a simple form with client-side Java Script validation.
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 Java Script function called That's all there is to simple Java Script form validation!
Shows you how to write a script that ensures your form is filled in correctly before it's sent to your server.
Great for "required fields" such as email addresses!
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: in any one of the above cases, if one or more of our checks fail, the form will not be sent to the server.So if you select the second item in the drop down, a text field associated with that selection appears.My issue is how to make that text field required, but only required based on drop down selection. I am writing a script that validates two select menus. 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. @eyefly: I'm guessing you're using cgiemail to process your form on the server?You can then adapt this form to your own requirements.Let's build a simple form with a validation script.One point to note about Java Script validation is that it can always be circumvented by the user disabling Java Script in their browser, so for secure validation you'll need to write your validating code in your server-side scripts.