[Javascript] Form validation

Chris Tifer christ at saeweb.com
Tue Jan 20 09:11:46 CST 2004


>         fieldName = form + '.' + arrFields[i];

That's probably your problem. You're building a string here.  Not a
reference to an element.

I wouldn't use "form" as a variable name either. I would use something else
as it either is a keyword or one day may become one.

>         sName = '' + arrFields[i] + '';

And this piece here... arrFields[i] is already a string. No need to add the
" around it.

One thing I'd like to point out though. Why are you building a list of field
names, or is this just the fields you want validated, omitting a few others?
If you want ALL fields in a form, you can do something like:

function valForm(strFormName){
    var objForm = document.forms[strFormName]

    for(var x = 0; x < objForm.elements.length; x++){
        var objEl = objForm.elements[x].name
        // you have a handle on the current element now and if you
        // add new elements they will automatically be included.
    }
}

Hope that gives you some more hints on how to tackle this.

Perhaps the first thing you should do is rename the 2nd parameter in your
function to something like "objForm" or "myForm" and see how that helps.

Chris Tifer




More information about the Javascript mailing list