GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialrepublic.com — JavaScript Form Validation - Tutorial Republic JAVASCRIPT BASIC
JS Introduction
JS Getting Started
JS Syntax
JS Variables
JS Generating Output
JS Data Types
JS Operators
JS Events
JS Strings
JS Numbers
JS If…Else
JS Switch…Case
JS Arrays
JS Sorting Arrays
JS Loops
JS Functions
JS Objects
JAVASCRIPT & DOM
JS DOM Nodes
JS DOM Selectors
JS DOM Styling
JS DOM Get Set Attributes
JS DOM Manipulation
JS DOM Navigation
JAVASCRIPT & BOM
JAVASCRIPT ADVANCED
JS Date and Time
JS Math Operations
JS Type Conversions
JS Event Listeners
JS Event Propagation
JS Borrowing Methods
JS Hoisting Behavior
JS Closures
JS Strict Mode
JS JSON Parsing
JS Error Handling
JS Regular Expressions
JS Form Validation
JS Cookies
JS AJAX Requests
JS ES6 Features
JAVASCRIPT EXAMPLES
JAVASCRIPT REFERENCE
Advertisements
JavaScript Form ValidationIn this tutorial you will learn how to validate an HTML form using JavaScript. Understanding Client-Side ValidationWeb forms have become an essential part of web applications. It is often used to collect user's information such as name, email address, location, age, and so on. But it is quite possible that some user might not enter the data what you've expected. So to save bandwidth and avoid unnecessary strain on your server resources you can validate the form data on client-side (i.e. user's system) using JavaScript before passing it onto the web server for further processing. Client-side validation is also helpful in creating better user experience, since it is faster because validation occurs within the user's web browser, whereas server-side validation occurs on the server, which require user's input to be first submitted and sent to the server before validation occurs, also user has to wait for server response to know what exactly went wrong. In the following section we will take a closer look at how to perform JavaScript form validation and handle any input errors found appropriately and gracefully. Note: Client-side validation is not a substitute or alternative for server-side validation. You should always validate form data on the server-side even if they are already validated on the client-side, because user can disable JavaScript in their browser. Form Validation with JavaScriptThe form validation process typically consists of two parts— the required fields validation which is performed to make sure that all the mandatory fields are filled in, and the data format validation which is performed to ensure that the type and format of the data entered in the form is valid. Well, let's get straight to it and see how this actually works. Creating the HTML FormLet's first create a simple HTML form that we will validate on client-side using JavaScript when the user clicks on the submit button. Well, let's create an HTML file named "application-form.html" and place the following code in it, then save it somewhere on your system.
Building the Form Validation ScriptNow we're going to create a JavaScript file that holds our complete validation script. Well, let's create a JavaScript file named "validator.js" and place the following code inside it, then save it at the same location where you've saved the previous HTML file. Go through each line of the following example code to understand how JavaScript validation works:
The value of an individual form field can be accessed and retrieved using the syntax
Also, to check whether the format of input data is correct or not we've used the regular expressions. It is one of the most effective techniques for validating the user inputs. Furthermore, the above script will also display the data entered by the user in an alert dialog box for preview purpose before submitting the form to the web server. Tip: However, you can validate email format using regular expression. But a user might enter an email that is correctly formatted but does not exist. So for authentic email validation, send confirmation email to the user and verify whether the email really exists or not. Adding Style Sheet to Beautify the FormFinally, create the file named "style.css" and place the following code in it, then save it also at the same location where you've saved the previous two files. These are the style rules to beautify our form. ExampleTry this code »
That's all, now open the "application-form.html" file in a web browser and try to fill some data and see how the script respond when an invalid data is entered in a form field. To learn about server-side validation, please check out the tutorial on PHP form validation Advertisements
Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |