Forum Moderators: open

Message Too Old, No Replies

Javascript not recognizing form field name

long alpha-numeric field names

         

SciFiScot

8:23 pm on Jun 6, 2005 (gmt 0)



I've found some really great answers in this forum to include the one I'm trying to get working right now. I've got to create forms that integrate into a CRM system and must use the field names given by that system. For some of our custom fields the CRM software uses names in the following format: 00N30000000xXxx. Scripts that work wonderfully with conventionally formatted field names (eg. field_name, field etc.) just don't work with these names.

For field validation I've been able to use "document.forms[x].elements[x].value" to accomplish my goal but I also wish to pre-fill field values with a name/value pair passed from the URL (mainly so I can create one form for many possible referring links rather than one each).

Here's the code (found in these fine forums) that I'm trying to make work with some of these funky field names:

<script>
function getIdFromQstring() {
var url = document.location+''; // Insures string
q=url.split('?');
if (q[1]) {
var pairs = q[1].split('&');
for (i=0;i<pairs.length;i++) {
var keyval = pairs[i].split('=');
if (keyval[0] == 'Field_name') { var v = keyval[1]; break; }
}
}
if (v) { return v; }

}
var Field_name = getIdFromQstring();
if (Field_name) { document.form_name.Field_name.value=Field_name; }
</script>

Just for simplicity I've used the field name as a variable name as well (follows the example I borrowed).

Can anyone think of a tricky way around these crazy field names?

Thanks!

ChadSEO

8:31 pm on Jun 7, 2005 (gmt 0)

10+ Year Member



Javascript indeed does not like it when variable names start with a number. If you can add an "x" or something at the beginning of the fieldname, that would probably be the easiest thing to do.

Since it sounds like you cannot, your best bet is probably going to be to loop through document.form_name.elements, and test if document.form_name.elements[i].name == Field_name. Not quite as effecient, but it should work for you.