Gabe,
That looks cool. I copied it into my syntax highllighting editor and formatted the spacing/tabbing to my own personal preferences, and gave it a quick look-see.
I am sick today (fever, sore throat), so I don't have the head about me to really dig in and give it a try yet.
I'll post back if I can get to it.
This script will validate form fields before submiting, just remember to back it up with php or some other SS script. Validates XHTML strict
/smile.gif" style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" />
Usage:
Requires: All forms must be named, Input nodes to validated are to be encapsulated in a div of the class of .required */
* Name: formSentry.js
* Purpose: To validate required form fields
* Date: 10/18/2005e
* Author: Gabe B. (christiangeek[nospam]@gmail.com), ...
*******************************************************************/
var reqFields = new Array;
//*** Function: checkInput, Purpose: Check values of all non-radio button inputs ***
function checkInput(obj)
{
//First check for a value
if (obj.value == "")
{
return "This field cannot be left empty";
}
//Does the field have a 'accept="command:parameters"'?
if(obj.accept) {
//If so
command = obj.accept;
//Split mutiple commands like this command:parameters;command:parameters
if(command.indexOf(";")) {command = command.split(";");}
for(var i=0;i<command.length;i++) {
//Split at :
params = command[i].split(":");
switch (params[0]) {
//*** Command: field, Purpose: Compares one field against another, Usage: 'accept="field:fieldname"'***
case "field":
fieldObj = document.getElementById(params[1]);
if(obj.value != fieldObj.value) {
return "This field does not match "+fieldObj.name;
}
break;
//*** Command: minimum, Purpose: Compares field length, Usage: 'accept="minimum:8"'***
case "minimum":
//What is the length?
if(obj.value.length < params[1]) {
return "This field must "+params[1]+" characters or longer";
}
break;
//*** Command: filter, Purpose: Checks the input against a filter, Usage: 'accept="filter:filtername"'***
case "filter":
switch(params[1]) {
//*** Parameter: email, Purpose: Tells if the input is a email, Usage: 'accept="filter:email"'***
case "email":
// Regular expression
var RE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
//Run regular expression
if (!RE.test(obj.value)) { return "Please enter a valid email "; }
break;
//*** Parameter: alpahanumeric, Purpose: Checks if the input contains only alphanumeric & _ characters, Usage: 'accept="filter:alphanumeric"'***
case "alphanumeric":
// Regular expression
var RE = /\W/;
//Run regular expression
if (RE.test(obj.value)) { return "Please use only letters, numbers, and underscore (_)"; }
break;
//*** Parameter: numeric, Purpose: Checks if the input contains only numeric characters, Usage: 'accept="filter:numeric"'***
case "numeric":
//No need for regular expressions
if (isNaN(obj.value)) { return "Please use only numbers"; }
break;
}
break;
}
}
}
return true;
}
//*** Function: checkRadio, Purpose: Check values of all radio button inputs ***
function checkRadio(obj)
{
//Loop through radios
for(var i=0;i<obj.length;i++)
{
//Check
if(obj[i].checked == true)
{
//Return cool
return true;
};
}
//Return a error code
return "Please check a button";
}
//*** Function: checkFields, Purpose: Determines whether the field is a radio button or not ***
function checkFields(obj)
{
//Clean up old errors
var errorDiv = document.getElementById("error");
if(errorDiv) {errorDiv.parentNode.removeChild(errorDiv); }
//Loop through required fields
for(var i=0;i<reqFields.length;i++)
{
obj = eval("document."+reqFields[i].formName+"."+reqFields[i].name);
//Is this field a radio button?
if(obj.length > 0)
{
//Run the checkRadio function
code = checkRadio(obj);
if(code != true)
{
//Tell the error handler which form did not register
var top = obj[0].parentNode.parentNode.parentNode;
};
}
//If not
else
{
//Run the checkRadio function
code = checkInput(obj);
if(code != true)
{
//Tell the error handler which form did not register
var top = obj.parentNode;
//Non-radio's can take focus, tell them here
var evalOBJ = "obj.focus()";
}
}
if (top) {
//Write out error info
error = document.createElement('div');
error.className = 'error';
error.id = 'error';
error.name = 'error';
//Change error string here
error.innerHTML ="ERROR: "+code+"";
top.appendChild(error);
//Evalute any functions
eval(evalOBJ);
return false;
}
}
return true;
}
/*** Function: formSentry, Purpose: Main validation function***
Usage: <script src="../includes/formSentry.js" language="javascript" type="text/javascript"/> </head> <body onLoad="formSentry()">
Requires: All forms must be named, Input nodes to validated are to be encapsulated in a div of the class of .required */
function formSentry()
{
var x = 0;
// Load all the forms
var formNode = document.getElementsByTagName('form');
for(var i=0;i<formNode.length;i++)
{
// Load all the divs in the form
var divNode = formNode[i].getElementsByTagName('div');
for(var i2=0;i2<divNode.length;i2++)
{
// Only divs with a class of .required
if (divNode[i2].className == "required")
{
//Get all input nodes
var inputNode = divNode[i2].getElementsByTagName('input');
// Loop through them
for(var i3=0;i3<inputNode.length;i3++)
{
// Check for duplicates
if (x > 0)
{
if (inputNode[i3].name == reqFields[x-1].name)
{
break;
}
}
// Write to array
reqFields[x] = inputNode[i3];
reqFields[x].formName = formNode[i].name;
x++;
}
}
}
}
// Assign onsubmit() on all the forms
for(var i=0;i<formNode.length;i++)
{
//Sweet!! We can now write the osubmit handeler
formNode[i].onsubmit = function() {return checkFields(this);}
}
}
Well its time to head out
Gotta find my place to rest
Before the sun goes down
In the darkness of the west - Day Of Fire