The easiest javascript form validation

Posted on April 16, 2008
Filed Under Design |

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

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • blinkbits
  • BlinkList
  • Fleck
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Slashdot
  • SphereIt
  • Spurl
  • StumbleUpon
  • Technorati

Comments

Leave a Reply