...

View Full Version : Checkbox validation won't work...



foundsheep
12-19-2007, 03:27 PM
I've tried several different variations but I have not been able to get the checkbox validation to work. Here is the code...

function validateThis(form1){
if (form1.Company.value == "" || form1.Company.value.length < 2){
alert("Please fill in your company name.");
form1.Company.focus();
return false;
}
if (form1.StreetAddress.value == "" || form1.StreetAddress.value.length < 2){
alert("Please fill in your Street Address.");
form1.StreetAddress.focus();
return false;
}
if (form1.ShipAddress.value == "" || form1.ShipAddress.value.length < 2){
alert("Please fill in your Ship Address.");
form1.ShipAddress.focus();
return false;
}
if (form1.BillAddress.value == "" || form1.BillAddress.value.length < 2){
alert("Please fill in your Billing Address.");
form1.BillAddress.focus();
return false;
}
if (form1.Email.value == "" || form1.Email.value.indexOf('@',1)== -1 || form1.Email.value.indexOf('.',2)==-1){
alert("Please fill in your email address.");
form1.Email.focus();
return false;
}
if (form1.City.value == "" || form1.City.value.length < 3){
alert("Please fill in your city.");
form1.City.focus();
return false;
}
if (form1.State.value == ""){
alert("Please fill in your State.");
form1.State.focus();
return false;
}
if (form1.Zip.value == "" || form1.Zip.value.length < 2){
alert("Please fill in your Zip.");
form1.Zip.focus();
return false;
}
if (form1.PhoneNumber.value == "" || form1.PhoneNumber.value.length < 2){
alert("Please fill in your Phone Number.");
form1.PhoneNumber.focus();
return false;
}
if (form1.FaxNumber.value == "" || form1.FaxNumber.value.length < 2){
alert("Please fill in your Fax Number.");
form1.FaxNumber.focus();
return false;
}
if (form1.AccountContact.value == "" || form1.AccountContact.value.length < 2){
alert("Please fill in your Account Contact.");
form1.AccountContact.focus();
return false;
}
if (form1.Buyer.value == "" || form1.Buyer.value.length < 2){
alert("Please fill in your Buyer.");
form1.Buyer.focus();
return false;
}
if (form1.chargeto[form1.chargeto.selectedIndex].value == "yes"){
if (form1.ExemptNumber.value == "") {
alert("Please enter your Tax Exempt Number.");
form1.ExemptNumber.focus();
return false;
}
return true;
}
if (form1.Bank.value == "" || form1.Bank.value.length < 2){
alert("Please fill in your Bank.");
form1.Bank.focus();
return false;
}
if (form1.Branch.value == "" || form1.Branch.value.length < 2){
alert("Please fill in your Bank Branch.");
form1.Branch.focus();
return false;
}
if (form1.BankContact.value == "" || form1.BankContact.value.length < 2){
alert("Please fill in your Bank Contact.");
form1.BankContact.focus();
return false;
}
if (form1.BankPhone.value == "" || form1.BankPhone.value.length < 2){
alert("Please fill in your Bank Phone Number.");
form1.BankPhone.focus();
return false;
}
var checkSelected = false;
for (i = 0; i < form1.gencon.length; i++)
{
if (form1.gencon[i].checked)
checkSelected = true;
}
if (!checkSelected)
{
alert("Please read the general conditions and check the box.");
return (false);
}

}

Thanks.

Philip M
12-19-2007, 05:58 PM
if (form1.Company.value == "" || form1.Company.value.length < 2){
is tautologous.

if (form1.Company.value.length < 2){
is sufficient.

but a lot of your validation is very basic. For example

if (form1.State.value == ""){
alert("Please fill in your State.");

So anything at all, however inappropriate, - even a space - will return true.


It is not very helpful to say simply that "it does not work". What actually happens and what error messages are generated?

How are you calling your validation script?

onclick = "return.validateThis(this.form)"

Checkboxes operate independently from other checkboxes and should all have unique names. They are not an array (as with radio buttons).

If your checkboxes are named gencom1, gencom2 and gencom3 then:

var checkSelected = false;
for (var i = 1; i <= 3; i++) {
if ( eval ( "form1.gencon" + i + ".checked" )) {
checkSelected = true;
}

In other words, at least one checkbox out of three must be checked.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum