Accessing form variable where variable name is a GUID

I am pulling questions out of a databse for an online survey.  The variable names in the html form are GUID's.

for example

<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">

I want to check the form variables with javascript, but when i try and access the variable:

document.formname.{cccccccc-cccc-cccc-cccc-cccccccccccc}.value

I get an error message.

I tried to assign the GUID to a variable as a string:
varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"

then access the form variable:
document.formname.varname.value

...but that didn't work either.

Any suggestions?

Thanks in advance
bizkit112Asked:
Who is Participating?
 
Ryan ChongCommented:
try use document.getElementById method ? like:

varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"

then:

document.getElementById(varname).value ...
0
 
ZvonkoSystems architectCommented:
The name has to be a String because of the special characters.
Then you can use that String in one of two ways:
Either:
document.formname["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value

Or:
document.formname[varname].value




0
 
Pravin AsarPrincipal Systems EngineerCommented:
Another way could be loop over (it is inefficient, but works).

<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
      alert (err);
}
onerror = catchErr;
function GetValue (form, fldName) {
  for (ix=0; ix < form.elements.length; ix++) {
     if (form.elements[ix].name.match(fldName)) {
      alert (form.elements[ix].value);
      return (form.elements[ix].value);
     }
  }
 
}
</script>
<form name="form1">
<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">
<input type="button" value="Get Value of GUID"
onclick='GetValue(this.form,"{cccccccc-cccc-cccc-cccc-cccccccccccc}");'>
</form>
</body>
</html>
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
ZvonkoSystems architectCommented:
Sorry, but what your GetValue() does is identical to: this.form["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value;
So why the error-prone loop? Don't you know that match() does also match a subset?
0
 
Pravin AsarPrincipal Systems EngineerCommented:
(form.elements[ix].name == fldName) does the exact match check.
Thanks Zvonko for helping me point out a possible flaw in my code.



<html>
<head>
<title>Form Field Eval</title>
</head>
<body>
<script language="javascript">
function catchErr(err) {
     alert (err);
}
onerror = catchErr;
function GetValue (form, fldName) {
  for (ix=0; ix < form.elements.length; ix++) {
     if (form.elements[ix].name == fldName) {
     alert (form.elements[ix].value);
     return (form.elements[ix].value);
     }
  }
 
}
</script>
<form name="form1">
<input type="text" name="{cccccccc-cccc-cccc-cccc-ccccccccccccccc}">
<input type="text" name="{cccccccc-cccc-cccc-cccc-cccccccccccc}">
<input type="button" value="Get Value of GUID"
onclick='GetValue(this.form,"{cccccccc-cccc-cccc-cccc-cccccccccccc}");'>
</form>
</body>
</html>
0
 
ZvonkoSystems architectCommented:
Flaw or not flaw, the loop is NOT necessary.
0
 
bizkit112Author Commented:
Here are the results I had...

when i use the following code:

varname = "{cccccccc-cccc-cccc-cccc-cccccccccccc}"
if (document.healthhistoryform.getElementByID(varname).value == '')

i get error: Object doesn't support this property or method

when i use:

if (document.healthhistoryform["{cccccccc-cccc-cccc-cccc-cccccccccccc}"].value == '')

i get the error:  '{GUID}.value' is null or not an object

and i know the form variable is not null.

Thanks for your help so far, are there any other suggestions?

0
 
ZvonkoSystems architectCommented:
Do you have the correct UpperCase and LowerCase spelling in healthhistoryform name and {cccccccc-cccc-cccc-cccc-cccccccccccc} field name? Best copied by copy'n paste.

0
 
bizkit112Author Commented:
Yes, the case is correct.
0
 
ZvonkoSystems architectCommented:
OK, the form element does not support the getElementByID() method, and even when you correct would call the document.getElementById() you cannot get a form element name when it has not the same id as name.
Can wee see your page source or at least the concerned parts? Or is your test page online?

0
 
Pravin AsarPrincipal Systems EngineerCommented:
I have tried my post ???

0
 
bizkit112Author Commented:
<script>
<!--

function checkForm()
{
      var validForm = false;
      alert("here");
      varname = "{66cd89ce-4326-43b9-a9ac-f3c5858901cc}"
      if (document.getElementByID(varname).value == '')
      {
            validForm = false;
            alert("it worked");
      }
      alert("after");
}


// -->
</script>

<form name="healthhistoryform" method="post" onsubmit="return checkForm()" action="#">
<table width="760" border="0" cellpadding="0" cellspacing="0">
      <tr>
            <td width="25"><%=i%></td>
            <td width="250"><%=HealthHistory("Question")%></td>
            <td><input type="<%=HealthHistory("Type")%>" name="<%=HealthHistory("GUID")%>" id="<%=HealthHistory("GUID")%>"></td>
      </tr>
</table>
</form>


Here, the GUID in the javascript function matches the HealthHistoryForm("GUID") value in the input field.
It did not work with the getElementByID, as mentioned before, possibly b/c it has the same value for name as it does for id.

I get the following error:
     Object doesn't support htis property or method

on this line of code:
     if (document.getElementByID(varname).value == '')
0
 
Ryan ChongCommented:
Javascript is case sensitive, try use document.getElementById instead of document.getElementByID, like:


function checkForm()
{
     var validForm = false;
     alert("here");
     varname = "{66cd89ce-4326-43b9-a9ac-f3c5858901cc}"
     if (document.getElementById(varname).value == '')
     {
          validForm = false;
          alert("it worked");
     }
     alert("after");
}
0
 
Ryan ChongCommented:
ah, this is same as what Zvonko already mentioned above.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.