Technology TidBits

Answers to various technical questions on php programming, mysql, linux, and many more categories.
25% off Hosting at HostGator.com:
Use Code techbits25



How can I validate a form that has brackets in the variable names?

Last updated: 06/21/2009

Sometimes when setting up forms, especially when using php , variable names use bracket notation, like: contact[name]. 

This causes problems when trying to validate the form using javascript.  A simple way to get around this is to use the bracket notation when checking form variables in javascript:



<SCRIPT language=javascript>
function validate() { 
  name=mainform['contact[name]'].value;
  if (name=="") {
    alert('Your Name is a required field.');
    return false;
  }
}
</SCRIPT>
X Of course, in your form, you should include the standard onsubmit="return validate();" parameter. 
blog comments powered by Disqus
Have your own Tech-bit to contribute? Submit it here

Other questions in this category:
How can I open a new window using Javascript?
How can I add a quick javascript error page to my cgi / php scripts
Why do my web forms lose data when I hit the back button?
How to refresh the parent window from a pop up window.
How can I submit data to a form from a pop-up window?
Configuring htmleditor to do file uploads using php
How can I easily hide / display a form or text on my website?
IE 7 gives me an "Unknown runtime error" when using ajax - how can I fix this?
How can I refresh the parent window from a pop-up
How to include Javascript into your html code



Powered by KnowledgebasePublisher 1.1
Host Gator
Content provided by Roberts WebForge, Inc.