Form validation on the client-side is essential - it saves time and bandwidth; you also have better control to show the user the wrong field. This comparison of different validation methods includes comments for improving portability and maintainability.Why is Client Side Validation Good
Two Major Validation Approaches
While displaying all errors is needed in case of server validation, the better method for client validation is to show one error at a time. This makes it possible to highlight the wrong field, making it a lot easier for the visitor. If you print all errors most people would also try to remember and correct them at once instead of trying to submit after each correction.
Considering these advantages I will focus only on validation methods that display one error at a time.
How to Validate Forms
Take for example the following code fragment:
<script type="text/javascript" language="javascript"> |
Note: JavaScript is case sensitive, the value which is assigned to the HTML id/name attribute is also case sensitive.
A better approach would be to add a form name:
function validateMyForm() { |
Let's remove the form name:
function validateMyForm(form) { |
Now how about making visitors' lives a lot easier and focus the field that triggered the error instead of making them find it on their own.
function validateMyForm(form) { |
That was pretty good, but don't you feel that it's a little too much code for every single field? If we create a simple library of functions we can save lots of typing and download time for the page.
Well next we'll do exactly this and define our basic functions that make validation code even shorter.
function validateNumber(field, msg, min, max) { |
We are passing the error message as a parameter to it, to use such a function we can basicly add it to the onsubmit handler like:
<form action="handler" |
Another method for validating numbers is to require them to be in a given range, to make the function do this kind of checks simply change the check line to:
if ((parseInt(field.value) != field.value) || |
<form action="handler" |
function validateString(field, msg, min, max) { |
A common field required in many forms on the web is the email, there're lots of functions I've seen but usually the simplest and easiest way to validate an email is using regular expressions.
We'll extend the function, making it possible to define the field as optional.
function validateEmail(email, msg, optional) { |
JavaScript cannot be used on its own for validation, but it helps a lot if you have it. The more compact code you embed in your HTML, the better - it saves download time and search engines will like you.
About the Author:
Martin Tsachev has been developing for the web since 2001. He's currently working as a web developer at Make-a-Store, Inc.. Martin's personal site mtWeb can be found at Martin.f2o.org.
Martin Tsachev has been developing for the web since 2001. He's currently working as a web developer at Make-a-Store, Inc.. Martin's personal site mtWeb can be found at Martin.f2o.org.Related Articles
- HTML Forms Validation On The Client SideForm validation on the client-side is essential - it saves time and bandwidth; you also have better control to show the user the wrong field. This comparison of different validation methods includes comments for improving portability and maintainability.
- Securing PHP Web ApplicationsCommon security holes and suggestions for resolving potential data compromise scenarios.
- Client Side ScriptingHowever, this is not to say that other technologies are not extremely useful. Several technologies have proven to be just as important as CGI for the average Internet developer. These technologies focus on putting the demands of computation in the hands of the client instead of the server...
- Why is Validation Important?Validation in your webpages is very important as proven in this tutorial. In this tutorial youll learn about Validation and how you should be making the effort to get validated!
- CSS Validation and HTMLHTML validation and CSS validation are controversial issues with some people. This article discusses some of the issues that have come increasingly to the fore in web development. The article will also provide a practical method that overworked webmasters can use to improve their website.
- Code Validation & Compliancy - The New Beginning XHTMLCode validation is still widely debated as to whether it is required for performance within the search engines. It is only a guess that the search engines dont utilise it within their algorithm, but nobody is actually 100% sure on that fact...
- 7 Benefits of HTML ValidationYou may not bother with html validation or writing simple and clean code when designing your web site. Later you may find your site is slow loading, appears incorrectly in the main browsers and does not rank well for the major search engines...
- Form ValidationLearn the different ways of validating your form with Javascript.
- What are XForms?HTML forms, such as email forms and order forms, are perhaps the most important components of a website. Without forms, webmasters would not be able to collect information from their site visitors, and they would not be able to accept online credit card orders from their customers...
- Why do you Need a Validated Website?This is a very good article for new designers or designers who are big on the visuals but not the programming. Why do you Need a Validated Website? - the author explains it really well.
