Script for validating online forms
For an alternative approach to client-side form validation, without Java Script, check out our new article on HTML5 Form Validation which is available now in most modern browsers.
When form input is important, it should always be verified using a secure server-side script.
Using client side Java Script is an efficient way to validate the user input in web forms.
When there are many fields in the form, the Java Script validation becomes too complex.
When the user does so, they may proceed and can supply value to the next available field.
The later Java Script function created is called on the onsubmit event of the form. If Male or Female is selected, it generates an alert saying that the form is successfully submitted and it reloads the form.
It sets the focus to the input field until the user supplies a valid value.
Let's build a simple form with a validation script.
It is important to validate the form submitted by the user because it can have inappropriate values. The Java Script provides you the facility the validate the form on the client side so processing will be fast than server-side validation.
Following pictorial shows in which field, what validation we want to impose.
We will create Java Script functions (one for each input field whose value is to validate) which check whether a value submitted by user passes the validation.