The easiest javascript form validation

Logo design tips

This week, I have been been creating an online form for a current client and I was looking for a Javascript form validation script. Surprise, Surprise I found the easiest one! And I thought I should share it.

This form validation script is so easy, it’s done in four easy steps.

1.You create the external .js file and include:

/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: wsabstract.com | http://www.wsabstract.com */
function checkrequired(which) {
var pass=true;
for (i=0;i<which.length;i++) {
var tempobj=which.elements[i];
if (tempobj.name.substring(0,8)==”required”) {
if (((tempobj.type==”text”||tempobj.type==”textarea”)&&
tempobj.value==”)||(tempobj.type.toString().charAt(0)==”s”&&
tempobj.selectedIndex==0)) {
pass=false;
break;
}
}
}
if (!pass) {
shortFieldName=tempobj.name.substring(8,30).toUpperCase();
alert(“The “+shortFieldName+” field is a required field.”);
return false;
} else {
return true;
}
}

2. Then you add the link to external script tag into the head of your document.

3. In your form you add the following:

<form onSubmit=”return checkrequired(this)”>

4. In the actual “name” of each individual field in the form, you add the word ‘required’ with no spaces in front of the current name.

It was so easy I thought it would never work.

Original form validation script here

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s