Home / Programming / Javascript / How can I validate a form that has brackets in the variable names?
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.