Forums

This topic is locked

Javascript question

Posted 02 Jul 2002 16:43:02
1
has voted
02 Jul 2002 16:43:02 Michael Rudge posted:
I have the following code:
function validate(form)
{
var returnVal=true;
var formEls=form.elements;
var currEl,currName,currType,currVal,currField,minimum,maximum,temp;
var errMsg="There were problems with the following field(s):\n";
var firstErr=-1;

var notWhitespace=/\S/;
var notAlpha=/[^a-z \-\.\,']/gi;
var notAlphaNumeric=/[^a-z0-9]/gi;
var notAddress=/[^\w \-#\.\,\/]/gi;
var hasSpaces=/\s/g;
var notInt=/\D/g;
var isDecimal=/^\d+(\.\d+)?$/;
var isCC=/^\d{4}(-\d{4}){3}$/;
var isUSZip=/^\d{5}(-\d{4})?$/;
var isUKPost=/[a-zA-Z]{1,2}[\d][1-9a-zA-Z]? [\d][a-zA-Z]{2}/;
var isUSPhone=/^\d{3}[-\.]\d{3}[-\.]\d{4}$/;
var isEmail=/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,6}(\.[a-z]{2})?$/i;
var isCurrency=/^\d+(\.\d{2})?$/;
var notComment=/[^a-zA-Z0-9\.\,;:%&#$@!\^-_~`"'\[\]\{\}\*\/\?\(\)]/i;

var requirements=new Array("required","alphabetic","address","alphanumeric","nospace",
"integer","decimal","minlength","maxlength","ccnumber","uszip","ukpost",
"usphone","email","currency","percent","comment";

for(var i=0;i<formEls.length;i++)
{
currEl=formEls[i];
currName=currEl.name;
currType=currEl.type;
currValue=currEl.value;
currField=currName.indexOf("_"!=-1?currName.split("_"[0]:currName;
currField=currField.replace(/0/g," ";
temp=0;
-this is not the end of it, but it is where the problem is. The declaration of var formEls=form.elements; does not seem to be populating the array. If I change that declaration to:var formEls=document.getElementsByTagName('input');
the script works, but there are some other focus issues. My question is what is the difference in these 2 statments? Can the first be fixed to read the form elements properly?
Thanks
Mike

Michael Rudge

Reply to this topic