I need to use javascript to validate 6 text inputs to make sure data is entered and they are not empty. However, there is a checkbox above this and if the checkbox is checked I need these areas to NOT validate as they become optional at that point.
The above is if the box is checked to validate the field. I want it to do the opposite. How would you do the above with the If statement referring if the check box is NOT checked? And how could I add more fields to check then just one?
I am new to javascript so any help or guidance is appreciated.
var textFields = ["name","address","city","zip","email","phone"]; // or whatever your text field names are
function validateForm( )
{
var form = document.form1;
if ( ! form1.sales.checked )
{
for ( var t = 0; t < textFields.length; ++t )
{
var field = form[textFields[t]];
var value = field.value.replace(/^\s+/,"").replace(/\s+$/,""); // trim the input
if ( value.length < 3 ) /* 3 is arbitrary...choose what you prefer */
{
oops += "\n" + field.name + " does not seem to be filled in";
}
}
if ( oops != "" )
{
alert("Please correct these errors:" + oops);
return false;
}
}
... other validation not dependent on that checkbox ...
}
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
var textFields = ["node","press","knocks","sales","contact","ws"];
function validateForm( )
{
var oops = ""; // must initialize this!
var form = document.wowsales;
if ( ! wowsales.off.checked )
...
You spelled "press" wrong. I omitted the initialization of the oops variable.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Thanks again for the help. Unfortunately, nothing happens when I submit the form. I submit with everything empty with and without the box checked and it works every time with no error.
I added the on submit to the form, what am I missing?