Skip to main content

Javascript form validation

One of the most popular uses of Javascript is when it comes to validating user input on a form. The most hateful way, though, is by not using onsubmit. A number of times I come across code where someone uses an image in place of the submit button and uses the onclick event which will validate then submit the form. Yeauch! Very bad practice!


The first thing to do with the form is place the Javascript function in the onsubmit attribute for the form, e.g.


<form name="reg" method="post" onsubmit="return validateForm();"

The use of return lets the form submit when the function returns true. If the function does not return true (i.e. false) then the form will not submit - as shown below.



function validateForm(){
return false;
}

Getting a reference to the form is the next step. There are a number of ways to do this:


  1. document.forms[0]

  2. document.forms['reg']

  3. document.getElementById('reg') This will work when id is used on the form

  4. Passing an object reference through the onsubmit handler

My preference is number 5. Passing the reference is achieved using the this keyword. So the onsubmit handler will look like


<form name="reg" method="post" onsubmit="return validateForm(this);"

and the function header would be. The function also demonstrates how a field is accessed


function validateForm(formObj){
alert(formObj.username.value);
return false;
}

As you can see this is much more straightforward and is pretty much cross-browser compatible. This has been tested with IE (6 and 7), Firefox (2 and 3), Google Chrome and Safari (v3 and v4) on the PC. And also tested on the Mac with Firefox 2 and Safari 2.

To actually do things with the form, a good tutorial exists over at quirksmode.org.

Comments

Popular posts from this blog

Get the most out of your browser

A whole year without a blog post! Now that I've changed job, I'm hoping that I can settle down and push more useful tips on my blog. So, to get back to form I have one about web browser productivity. Web browsers have come a long way since it's inception in the early days of the World Wide Web. But if you've just been using it to browse the Web, you are really missing out on a ton of stuff. Productivity is just the start of it. Let's take a look at some of the hidden gems that a browser has to offer. I will mostly be focusing on Chrome and Firefox, but a lot (if not all) the tips may apply to your favourite browser. Pin Tabs First up is the ability to pin a tab. You do this by right-clicking on a tab and selecting Pin Tab . Did you just see how it react? It resize and just shows the icon. If you have a lot of tabs this is a real space saver. Using Keywords Firefox has keyword bookmarking, which is an absolute essential for many. This allows you to call a

Open Source alternatives to Adobe Creative Suite

With the take over of Macromedia by Adobe, they have increased their arsenal of design packages, as well as the price. No-one can do pretty much without the popular package of Photoshop for their images, and Dreamweaver for websites, especially aspiring design students who do not have much money. Nevertheless, open source software has come to save the day. What is open source software? In a nutshell it is free software with its source-code freely available to those who would like to expand or improve on the software. To expand further, anyone who’s tinkered with the source code such as optimisation or additional functionality can resubmit it back to the holders and let others enjoy the fruits of your labour. I have trawled through the World Wide Web to find out how everyone can benefit from Open Source to build up their own studio and compete against Adobe Creative Suite. All this for free, apart from the cost of the Internet. I am planning to have a facility where you can purchase

More social networking concerns

Yeah. I'm not gonna leave you alone about this. I want to drive another point to you about social networking. After chatting to a few "savvy" Facebook users, I can't help but notice how much they don't realise that the social network has grown. Looking back in time, in 2012 Facebook announced they had a billion monthly active users. Fast forward to August 2015, they broke news that a billion people visited Facebook on a particular Monday. Then in September it was announced that the service averages over 1 billion users per day. While that paints a nice pretty picture there's more to it than posting onto your Facebook wall. They have made some pretty big and newsworthy acquisitions like WhatsApp , Oculus VR and Instagram . See where I'm going? It gets so addictive to make "friends" online. They may not be real friends you see and share life with everyday, but just someone you choose to bloat to about something. Sorry if I come across rud