Click to See Complete Forum and Search --> : Radio buttons and Check boxes validation problem
Illufox
06-26-2003, 11:37 AM
I'm working on a form for a client website. I added a validation script. The validation works fine until I add the validation for the radio buttons and checkboxes. I tried different scripts and methods but nothing helps. I don't get any errors but the form submits without validating the radio buttons and checkboxes. This is extremely frustrating and confusing.
Here is the form: http://www.illufoxdesign.com/MBCW_form.html
Any help is greatly appreciated.
Illufox
06-26-2003, 03:40 PM
This validation is only partially working (everything but radio buttons and checkboxes is validated) - why?
<script Language="JavaScript">
// Check if no empty fields -------------->
function validRequired(formField,fieldLabel)
{
var result = true;
if (formField.value == "")
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}
return result;
}
// Check if textboxes filled out -------------->
function validRequired2(formField,fieldLabel)
{
var result = true;
if (formField.value == "")
{
alert('Please fill out the "' + fieldLabel +'" text box.');
formField.focus();
result = false;
}
return result;
}
// Check Zip Code -------------->
function validZip(formField,fieldLabel)
{
var result = true;
if (document.form.zip.value.length != 5)
{
alert("Please enter a valid zip code");
formField.focus();
result = false;
}
return result;
}
// Check Phone Number -------------->
function validPhone(formField,fieldLabel)
{
var result = true;
var GoodChars = "0123456789()-+ "
var i = 0
if (formField.value == "")
{
alert('Please enter a value for the "' + fieldLabel +'" field.');
formField.focus();
result = false;
}
for (i =0; i <= formField.value.length -1; i++)
{
if ((GoodChars.indexOf(formField.value.charAt(i)) == -1) || (formField.value.length <10))
{
alert("Please enter a valid phone number with area code")
formField.focus();
result = false;
}
}
return result
}
// Check Email -------------->
function isEmailAddr(email)
{
var result = false;
var theStr = new String(email);
var index = theStr.indexOf("@");
if (index > 0)
{
var pindex = theStr.indexOf(".",index);
if ((pindex > index+1) && (theStr.length > pindex+1))
result = true;
}
return result;
}
function validEmail(formField,fieldLabel,required)
{
var result = true;
if ((formField.length < 3) || !isEmailAddr(formField.value))
{
alert('Please enter a complete email address in the form: yourname@yourdomain.com');
formField.focus();
result = false;
}
return result;
}
// Check radios -------------->
function radios()
{
var result = true;
if(document.form.contact[0].checked==false && document.form.contact[1].checked==false)
{
alert('Please select one contact method');
result = false;
}
return result;
}
// Check checkboxes -------------->
function daysChecked()
{
var result = true;
if((document.form.availability[0].checked==false) &&
(document.form.availability[1].checked==false) &&
(document.form.availability[2].checked==false) &&
(document.form.availability[3].checked==false) &&
(document.form.availability[4].checked==false) &&
(document.form.availability[5].checked==false) &&
(document.form.availability[6].checked==false))
{
alert('Please select at least one day');
result = false;
}
return result;
}
// Check All -------------->
function validateForm(theForm)
{
if (!validRequired(theForm.name,"Full Name"))
return false;
if (!validRequired(theForm.address,"Address"))
return false;
if (!validRequired(theForm.city,"City"))
return false;
if (!validZip(theForm.zip,"Zip"))
return false;
if (!validPhone(theForm.phone,"Phone number"))
return false;
if (!validEmail(theForm.email,"Email Address",true))
return false;
if (!radios)
return false;
if (!daysChecked)
return false;
if (!validRequired2(theForm.interest,"Interests and special skills"))
return false;
if (!validRequired2(theForm.motivation,"Motivation for volunteering"))
return false;
return true;
}
</script>
Illufox
06-26-2003, 05:51 PM
No, I used HomeSite which reports all errors.
When I check the javascript in NS I get an error report but it doesn't make any sense to me, since it doesn't give me a clue.....
It looks like it doesn't like the checkbox validation......I can't see anything wrong with it.....
Illufox
06-27-2003, 11:07 AM
Here we go:
Error: redeclaration of const kIOServiceProgID
Source File: chrome://communicator/content/utilityOverlay.js
Line: 1
Illufox
06-27-2003, 05:59 PM
No, not at all. I don't even know where this comes from! When you go to the form page at: http://www.illufoxdesign.com/MBCW_form.html
you will see that there's no such thing.
Somehow this thread has gone into a wrong direction. All I really want is that somebody looks at my validation script and tells me why the heck my checkbox and radiobutton validation seems to be ignored when the form is submitted. Everthing else works just fine.
This is the part I'm referring to:
// Check radios -------------->
function radios()
{
var result = true;
if(document.form.contact[0].checked==false && document.form.contact[1].checked==false)
{
alert('Please select one contact method');
result = false;
}
return result;
}
// Check checkboxes -------------->
function daysChecked()
{
var result = true;
if((document.form.availability[0].checked==false) &&
(document.form.availability[1].checked==false) &&
(document.form.availability[2].checked==false) &&
(document.form.availability[3].checked==false) &&
(document.form.availability[4].checked==false) &&
(document.form.availability[5].checked==false) &&
(document.form.availability[6].checked==false))
{
alert('Please select at least one day');
result = false;
}
return result;
}
The final function includes the following (part):
if (!radios)
return false;
if (!daysChecked)
return false;
It's better if you look at the whole code by going to the page I referred to or by looking at the code I pasted into a posting above.
I'm sure it's only a minor glitch I miss, I just can't see the error....
Homesite doesn't give me any errors either, so it can't be a syntax error. It must be more a logical error.....
:(
Illufox
06-28-2003, 04:06 PM
Dave, you nailed it down! Thank you, thank you, thank you!!!!!!!
All that was missing was another set of ()!!!! I also changed the style of the script as you suggested.
The validation works perfect now! Awesome, you made my day!!!!