Jump to content

[SOLVED] User form id instead of form name


acctman

Recommended Posts

i'm using this script code, i need to use the form field id's instead of the name. how can I do that?

 

<script type="text/javascript">
function validate(form) {
  var city = form.city.value;
  var zip = form.zip.value;
  if(city == "") {
    inlineMsg('city','<strong>Error:</strong><br />Enter your city.',6);
    return false;
  }
  if(zip == "") {
  inlineMsg('zip','<strong>Error:</strong><br />Enter your US or CAN Zip.',6);
  return false;
  }
  return true;
}
</script>

??? Sorry, I'm not following.

 

form.city.value; looks for the form field with "name=city" in it. i'd like for it to look for the "id" instead, since I have other code that requires the form field name to be formated a name=add[city] in which the javascript can not detect. i tried changing form.city.value to document.getElementById(city) and putting id=city in the form input code but that didn't work

<script type="text/javascript">
function validate(form) {
  var city = form.getElementById("city").value;
  var zip = form.getElementById("zip").value;
  if(city == "") {
    inlineMsg('city','<strong>Error:</strong><br />Enter your city.',6);
    return false;
  }
  if(zip == "") {
     inlineMsg('zip','<strong>Error:</strong><br />Enter your US or CAN Zip.',6);
     return false;
  }
  return true;
}
</script>

<script type="text/javascript">
function validate(form) {
  var city = form.getElementById("city").value;
  var zip = form.getElementById("zip").value;
  if(city == "") {
    inlineMsg('city','<strong>Error:</strong><br />Enter your city.',6);
    return false;
  }
  if(zip == "") {
     inlineMsg('zip','<strong>Error:</strong><br />Enter your US or CAN Zip.',6);
     return false;
  }
  return true;
}
</script>

 

thanks that worked, but i had to use document. instead of form.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.