Quantcast

Validating HTML Data with JavaScript

JavaScript can be used to validate data in HTML forms before sending off the content to a server. Form data that typically are checked by a JavaScript could be:

– has the user left required fields empty?
– has the user entered a valid e-mail address?
– has the user entered a valid date?
– has the user entered text in a numeric field?

First of all, all the required fields are checked in the form. For example, the function below checks if a field has been left empty. If the field is blank, an alert box alerts a message, the function returns false, and the form will not be submitted:

An alternate way is to call the function when the form is being submitted:

JavaScript is also used to check whether the content of an email is valid or not. This means that the input data must contain an @ sign and at least one dot (.). Also, the @ must not be the first character of the email address, and the last dot must be present after the @ sign, and minimum 2 characters before the end. The function to perform these tasks is as follows:

Like simple forms, this function can also be called when the email is being sent:

With JavaScript validation, user can save time since if not validated; they would have to change the data later when they come to know about its absence.

Looking for a quality hosting service? here are a few I suggest.
BlueHost | HostGator | Media Temple
Admin
Admin
Ali has been an entrepreneur in web, video and related technologies. Having worked with many business across the globe, Ali stands truly a great pillar in the business working with him.