Basic Validation
JavaScriptSource Staff Apr 14, 1999
General
This is a simple method to require your visitors to fill out certain fields in a form. Just add the word "required" to each required field's name and your visitor must fill it out to submit the form.
Source Code
Paste this source code into the designated areas.
External file
Paste this code into an external JavaScript file named: basicValidation.js
Select code
/* This script and many more are available free online at
The JavaScript Source!! http://www.javascriptsource.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;
}
}
Head
Paste this code into the HEAD section of your HTML document.
Select code
<script type="text/javascript" src="basicValidation.js"></script>
Body
Paste this code into the BODY section of your HTML document
Select code
<form onSubmit="return checkrequired(this)">
<table>
<tr>
<td>Name:</td><td><input type="text" name="requiredname"></td>
</tr><tr>
<td>E-mail:</td><td><input type="text" name="requiredemail"><td>
</tr><tr>
<td></td><td>
<select name="requiredhobby">
<option selected>Pick an option!
<option>1</option>
<option>2</option>
<option>3</option>
</select>
</td>
</tr><tr>
<td>Comments:</td><td><textarea name="requiredcomments"></textarea></td>
</tr>
</table>
<input type=submit value="Submit">
</form>
Leave a Response
(0 comments)