Ok, I am hoping that this will help you guys to help me 🙂
The suggestions did not work (i could have done it wrong though - so please check me).
This is the set-up:
FORM (rather long- so just including the part that is relevant to this question)
$formvalidationarray is created BEFORE the header (which holds almost all the beginning HTML for the site) because it is used in that file for calling the form validation stuff for javascript - which is included as an external file.
$formvalidationarray = "
<script language = 'javascript' type = 'text/javascript'>
var field = new Array();
var check = new Array();
var disp = new Array();
field[0] = ['first_name'];
check[0] = ['required'];
disp[0] = ['First Name'];
field[1] = ['last_name'];
check[1] = ['required'];
disp[1] = ['Last Name'];
field[2] = ['username'];
check[2] = ['required'];
disp[2] = ['Username'];
</script>";
include("../header.php");
?>
<div id = "formbox"></div>
<div class = 'liteform'>
<h2>CREATE EMPLOYEE</h2>
<?php echo $form_message; ?>
<form name = 'employee_action' method = 'POST' action = '<?php echo $_SERVER['PHP_SELF'];?>' onSubmit = "return formValidator(field,check,disp);">
JAVASCRIPT FILE (which is included in the header.php code in the html head tags)
function formValidator(fieldsarray,displaysarray,checksarray)
{
var checkToMake;
var field;
var displayField;
var flag = false;
var errorMessages = new Array();
for(var i = 0; i < fieldsarray.length; i++)
{
field = fieldsarray[i];
displayField = displaysarray[i];
checkToMake = checksarray[i];
alert(field + " " + displayField + " " + checkToMake + "\n\n");
switch(checkToMake)
{
case 'required':
if(isEmpty(field.value))
{
flag = false;
var thisstr = displayField + " is a required field";
errormessages.push(thisstr);
}//end if
break;
case 'number':
var numericExpression = /^[0-9]+$/;
if(!field.value.match(numericExpression))
{
flag = false;
var thisstr = displayField + " can only contain numbers";
errorMessages.push(thisstr);
}//end if
break;
case 'alphaonly':
var alphaExp = /^[a-zA-Z]+$/;
if(!field.value.match(alphaExp))
{
flag = false;
var thisstr = displayField + " can only contain letters";
errorMessages.push(thisstr);
}//end if
break;
case 'alphanumeric':
var alphaExp = /^[0-9a-zA-Z]+$/;
if(!field.value.match(alphaExp))
{
flag = false;
var thisstr = displayField + " can only be a combination of numbers and letters";
errorMessages.push(thisstr);
}//end if
break;
case 'email':
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(!field.value.match(emailExp))
{
flag = false;
var thisstr = displayField + " is not a valid email address";
errorMessages.push(thisstr);
}//end if
break;
case 'phonenumber':
//var re = /^\(?[2-9]\d{2}[\)\.-]?\s?\d{3}[\s\.-]?\d{4}$/
var phoneExp = /^\(?[2-9]\d{2}[-]?\s?\d{3}[\s\.-]?\d{4}$/
if(!field.value.match(phoneExp))
{
flag = false;
var thisstr = displayField + "is not a valid phone number FORMAT: 000-000-0000";
errorMessages.push(thisstr);
}//end if
break;
}//end Switch
}//end for loop
if(flag == false)
{
var str1 = "<ul>";
var str2 = "";
for(var i = 0; i <= errorMessages.length; i++)
{
str2 += "<li>" + errorMessages[i] + "</li>";
}//end forloop
var str3 = str1 + str2 + "</ul>";
alert(str3);
return false;
}//end if
else
{
alert("RETURNING TRUE");
return true;
}//end else
}//end Function
So here is what is and is not working -
The validation array created in the form HTML is working (verified through an alert)
The javascript.js file is being included properly (verified through an alert)
The function formValidator is being called (verified by an alert)
The for loop inside that function is being entered (verified by an alert)
The for loop parameters are being seen correctly (which are the arrays from the HTML) - verified by that alert you see insode the for loop and alerted when you enter the loop each time.
The switch is NOT working - tried alerting when the required case is supposed to be called - no alert is given
The errorMessages array is NOT being populated (of course probably because the switch is not working).
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Any help would be appreciated here - this is an intranet site for work that I am SLOWLY progressing on because of this sinlge form validation scheme is NOT working - I have spent a week and a half writing and rewriting this - please help i am losing my mind here. Also - if you have a better form validation scheme, please let me know 🙂 - but this one is simple and it took me forever to figure out something that worked in javascript LOL.