Javascript Validation using a JS SRC Tag

Author: Tripp W Black

Created: 04/25/2002 at 02:20 PM

 

Category:
General Web Tips
JavaScript

Add to top of form:
______________________________________

<SCRIPT LANGUAGE="JavaScript">
//List of required fields:
var Fields = new Array("ShortDesc", "ProblemDesc", "NewPriority", "Severity", "System", "ProjectPhase", "RequireTestingYN");

//List of mesages:
var Messages = new Array();
Messages[0]="You must enter a Short Description for this issue."; //ShortDesc
Messages[1]="You must enter a Problem Description for this issue."; //ProblemDesc
Messages[2]="You must select a Priority for this issue."; //Priority
Messages[3]="You must select a Severity for this issue."; //Severity
Messages[4]="You must select a System for this issue."; //System
Messages[5]="You must select a Project Phase for this issue."; //ProjectPhase
Messages[6]="You must indicate if Testing is Required."; //RequireTestingYN

//List of default values:
var DefaultValues = new Array();
DefaultValues[0]=""; //ShortDesc
DefaultValues[1]=""; //ProblemDesc
DefaultValues[2]="Unknown"; //Priority
DefaultValues[3]=""; //Severity
DefaultValues[4]="---Select System---"; //System
DefaultValues[5]="Unknown"; //ProjectPhase
DefaultValues[6]=""; //RequireTestingYN
</SCRIPT>

___________________________________________________________

Add to HTMLHead:
dbPath := @ReplaceSubstring(@Subset(@DbName; -1);" " : "\\";"+" : "/");
"<script src=\"/" + dbPath + "/Validation.js\"></script>" +
"<META HTTP-EQUIV=expires CONTENT=0>" +
"<META HTTP-EQUIV=pragma CONTENT=no-cache>"

___________________________________________________________

Kick this off with the following Javascript action button:
if(valid( Fields, Messages, DefaultValues)) document.forms[0].submit()

___________________________________________________________

Create the following page named validation.js:

=======================validation.js========================
function valid( Fields, Messages, DefaultValues) {
// This function validates the content of fields depending on Their types
var frm = window.document.forms[0];

for (var i=0; i<Fields.length ; i++) {
var FType=eval ( 'frm.'+Fields[i]+'.type' ) ;
var FName=eval ( 'frm.'+Fields[i]+'.name' ) ;
switch (FType)
{
case "text" :
if ( eval ( 'frm.'+Fields[i]+'.value' ) == DefaultValues[i]) {
alert(Messages[i]);
eval('frm.'+Fields[i]+'.focus()');
return false;
}
break

case "textarea" :
if ( eval ( 'frm.'+Fields[i]+'.value' ) == DefaultValues[i]) {
alert(Messages[i]);
eval('frm.'+Fields[i]+'.focus()');
return false;
}
break

case "select-one" :
chk = 'false';
if ( eval ( 'frm.'+Fields[i]+'.options[frm.'+Fields[i]+'.selectedIndex].text') !=DefaultValues[i]) {
chk = 'true';
}
if (chk=='false') {
alert(Messages[i]);
eval('frm.'+Fields[i]+'.focus()');
return false
}
break

case "select-multiple":
if (eval ( 'frm.'+Fields[i]+'.selectedIndex')==-1) {
alert(Messages[i]);
return false
}
break

case "hidden":
break

default:
switch (eval ( 'frm.'+Fields[i]+'[0].type' ) ) {
case "radio" :
chk = 'false';
for (var m=0; m < eval ('frm.'+Fields[i]+'.length') ; m++) {
if (eval('frm.'+Fields[i]+'['+m +'].checked')) {
chk = 'true';
}
}
if (chk=='false') {
alert(Messages[i]);
return false
}
break

case "checkbox":
chk = 'false';
for (var m=0; m < eval ('frm.'+Fields[i]+'.length') ; m++) {
if ( eval ( 'frm.'+Fields[i]+'['+m +'].checked') ) {
chk = 'true';
}
}
if (chk=='false') {
alert(Messages[i]);
return false
}
break
} //This closes the switch
} //This closes the switch
} //This closes the for Loop
return true
} //This closes the function

previous page