Page 1 of 1

Javascript on IE, elements[fieldName], dynamic fieldName

Posted: Wed Feb 11, 2009 8:34 am
by chopsmith

Code: Select all

 
function validateForm_addCase_form(formObject,numClaimants) {
    alertText = "";
        
    for(i=0;i<numClaimants;i++) {
        fieldName = i + "_claimant_ssn";
        if(formObject.elements[fieldName].value.length > 0) {
            //strip out all non-numeric characters
            formObject.elements[fieldName].value = formObject.elements[fieldName].value.replace(/[^0-9]/g, '');
            if(formObject.elements[fieldName].value.length != 9) {
                alertText += "Please enter a valid, 9-digit Social Security Number for Claimant Number " + (i + 1) + ".\n";
            }
            else {
                formObject.elements[fieldName].value = formObject.elements[fieldName].value.substring(0,3) + "-" + formObject.elements[fieldName].value.substring(3,5) + "-" + formObject.elements[$ieldName].value.substring(5,9);
            }
        }
        else {
            //do nothing related to ssn, because nothing was entered and it's not required
        }
    }
 
Works fine on FF, but not IE. Any idea why? If I alert(formObject.elements[fieldName].name), it gives me the names of the first i (counter) fields in the form, rather than the first i fields that follow the i_claimant_ssn naming. Again, works as expected on FF. I hate IE!

Re: Javascript on IE, elements[fieldName], dynamic fieldName

Posted: Wed Feb 11, 2009 9:15 am
by chopsmith
Now, I see that accessing the form element in this manner works in IE and not in FF:

Code: Select all

 
field = formObject(fieldName); //ie
 
While accessing it in this manner works in FF but not IE:

Code: Select all

 
field = formObject.elements[fieldName]; //ff
 
Does anyone know of one way that works for both? Or, will I have to check the browser type each time? I find it hard to believe that such a simple task is not supported congruently in both browsers.

Re: Javascript on IE, elements[fieldName], dynamic fieldName

Posted: Wed Feb 11, 2009 9:21 am
by chopsmith
So, I ended up doing the following, but I hate it. Thought I'd just put it up here for anyone it may help:

Code: Select all

 
function validateForm_addCase_form(formObject,numClaimants) {
    alertText = "";
        
    //deal with claimant_ssn(s)
    for(i=0;i<numClaimants;i++) {
        fieldName = i.toString() + "_claimant_ssn";
        if (navigator.appName == "Netscape") {
            field = formObject.elements[fieldName]; //ff
        }
        if (navigator.appName == "Microsoft Internet Explorer") {
            field = formObject(fieldName); //ie
        }
        if(field.value.length > 0) {
            //strip out all non-numeric characters
            field.value = field.value.replace(/[^0-9]/g, '');
            if(field.value.length != 9) {
                alertText += "Please enter a valid, 9-digit Social Security Number for Claimant Number " + (i + 1) + ".\n";
            }
            else {
                field.value = field.value.substring(0,3) + "-" + field.value.substring(3,5) + "-" + field.value.substring(5,9);
            }
        }
        else {
            //do nothing related to ssn, because nothing was entered and it's not required
        }
    }
 
Also, did anyone know that "navigator.appName" returns "Netscape" for FF? Thought that was strange.