Hi all,

Can i know how to do the checkbox validation........
i used the code below but its not validating.....

var val;	
	for(i=0;i<3;i++)
	{
		if(form2.elements['contype[]'][i].checked)
			val=1;
	}
	if(val==0)
	{
		alert("Select type of your Contract")
		return false;
	}
if ( document.Form2.elements.contype.checked == false )
    {
        alert ( "Please Select type of your contract." );
        valid = false;
    }

Try this:

for(var i=0; i < d.c.length; i++){
			if(d.c[i].checked) {
				total +=d.c[i].value + "\n";
				d.check_compare.value=d.check_compare.value+d.c[i].value+',';
			}
		}
		if(d.check_compare.value=="") {
		 alert("Please select atleast one check Box");
		 return false;
		}

K...

Hi just go through the code, its validating for all the filelds except checkbox.....

function val_cont()
{
	var ValidChars = "0123456789.";
	if(form2.emailid.value == "")
	{
		alert("Enter your Email ID")
		form2.emailid.focus()
		return false
	}
	if(form2.emailid.value!="")
	{
		var e=/^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-Z0-9]{2,4}$/;
		if(!form2.emailid.value.match(e))
		{
			alert("Enter valid Email ID");
			form2.emailid.select();
			return false;
		}
	}
	if(form2.password.value=="")
	{
		alert("Enter your passwordword");
		form2.password.focus();
		return false;
	}
	for(i=0;i<form2.password.value.length;i++)
	{
		var chki =" @#$%^&*()';:><.,+-_";
		if (chki.indexOf(form2.password.value.charAt(i)) != -1)
		{
			alert("Enter valid passwordword")
			form2.password.value=""
			form2.password.focus()
			return false
		}
	}
	if(form2.conpass.value=="")
	{
		alert("Enter your Confirm passwordword");
		form2.conpass.focus();
		return false;
	}
	if(form2.password.value!= form2.conpass.value)
	{
		alert("passwordword and Confirm passwordword must be same");
		form2.password.value=""
		form2.conpass.value=""
		form2.password.focus();
		return false;
	}
	
	if(form2.businessname.value=="")
	{
		alert("Enter the businessname")
		form2.businessname.focus()
		return false
	}
	
	if(form2.businessname.value.charCodeAt(0)==32)
	{
		alert("Enter valid businessname")
		form2.businessname.select()
		return false
	}
	if(form2.streetaddress.value=="")
	{
		alert("Enter Address")
		form2.streetaddress.select()
		return false
	}
	if(form2.streetaddress.value.charCodeAt(0)==32)
	{
		alert("Enter valid streetaddress")
		form2.streetaddress.select()
		return false
	}
	
	if(form2.zip.value=="")
	{
		alert("Enter zipcode Code")
		form2.zip.select()
		return false
	}
	if(form2.zip.value.length<5)
	{
		alert("Enter valid zipcode Code");
		form2.zip.select();
		return false;
	}
	if(form2.zip.value!="")
	{
		for (i = 0; i < form2.zip.value.length; i++)
		{
			Char = form2.zip.value.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				alert("Enter valid zipcode Code");
				form2.zip.select();
				return false;
			}
		}
	}
	if(form2.phoneareacode.value=="")
	{
		alert("Enter Phone Number")
		form2.phoneareacode.select()
		return false
	}
	if(form2.phoneareacode.value!="")
	{
		for (i = 0; i < form2.phoneareacode.value.length; i++)
		{
			Char = form2.phoneareacode.value.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				alert("Enter Valid Phone Number");
				form2.phoneareacode.select();
				return false;
			}
		}
	}
	if(form2.phoneprefix.value=="")
	{
		alert("Enter Phone Prefix")
		form2.phoneprefix.select()
		return false
	}
	if(form2.phoneprefix.value!="")
	{
		for (i = 0; i < form2.phoneprefix.value.length; i++)
		{
			Char = form2.phoneprefix.value.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				alert("Enter valid Phone Prefix");
				form2.phoneprefix.select();
				return false;
			}
		}
	}
	if(form2.phonesuffix.value=="")
	{
		alert("Enter Phone Suffix")
		form2.phonesuffix.select()
		return false
	}
	if(form2.phonesuffix.value!="")
	{
		for (i = 0; i < form2.phonesuffix.value.length; i++)
		{
			Char = form2.phonesuffix.value.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				alert("Enter valid Phone Suffix");
				form2.phonesuffix.select();
				return false;
			}
		}
	}
	if(form2.faxareacode.value=="")
	{
		alert("Enter Fax Number")
		form2.faxareacode.select()
		return false
	}
	if(form2.faxareacode.value!="")
	{
		for (i = 0; i < form2.faxareacode.value.length; i++)
		{
			Char = form2.faxareacode.value.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				alert("Enter Valid Fax Number");
				form2.faxareacode.select();
				return false;
			}
		}
	}
	if(form2.faxprefix.value=="")
	{
		alert("Enter Fax Prefix")
		form2.faxprefix.select()
		return false
	}
	if(form2.faxprefix.value!="")
	{
		for (i = 0; i < form2.faxprefix.value.length; i++)
		{
			Char = form2.faxprefix.value.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				alert("Enter valid Fax Prefix");
				form2.faxprefix.select();
				return false;
			}
		}
	}
	if(form2.faxsuffix.value=="")
	{
		alert("Enter Fax Suffix")
		form2.faxsuffix.select()
		return false
	}
	if(form2.faxsuffix.value!="")
	{
		for (i = 0; i < form2.faxsuffix.value.length; i++)
		{
			Char = form2.faxsuffix.value.charAt(i);
			if (ValidChars.indexOf(Char) == -1)
			{
				alert("Enter valid Fax Suffix");
				form2.faxsuffix.select();
				return false;
			}
		}
	}
	

	if(form2.contactfirstname.value=="")
	{
		alert("Enter your Contract Lastname");
		form2.contactfirstname.focus();
		return false;
	}
	for(i=0;i<form2.contactfirstname.value.length;i++)
	{
		var chki =" @#$%^&*()';:><.,+-_";
		if (chki.indexOf(form2.contactfirstname.value.charAt(i)) != -1)
		{
			alert("Enter valid Firstname")
			form2.contactfirstname.select()
			return false
		}
	}

	if(form2.contactlastname.value=="")
	{
		alert("Enter your Contract Lastname");
		form2.contactlastname.focus();
		return false;
	}
	for(i=0;i<form2.contactlastname.value.length;i++)
	{
		var chki =" @#$%^&*()';:><.,+-_";
		if (chki.indexOf(form2.contactlastname.value.charAt(i)) != -1)
		{
			alert("Enter valid Lastname")
			form2.contactlastname.select()
			return false
		}
	}

	

}

  function main_method()
	{		
	    if((!val_cont()) || (!check()) )
	    {
	        return false;

	    }
	    else
	    {
	        return true;

	    }
	}  

	
	 function check()

   {
   
      
      for(var i=0; i < form2.contype.length; i++){
   
      if(form2.contype[i].checked) {
   
      total +=form2.contype[i].value + "\n";
   
      form2.check_compare.value=form2.check_compare.value+form2.contype[i].value+',';
   
      }
   
      }
   
      if(form2.check_compare.value=="") {
   
      alert("Please select atleast one check Box");
   
      return false;
  
      }

}

Hey use this code where your checkbox validtion goes:

<script language="javascript">
function check()
{
	var d=document.checkoutform;
	if(d.confirm1.checked==false) {alert("Please confirm your details are correct!!");
	return false;
	}
  	else
	{return true;}
		
}
function trimstr(str) {
	str= this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
                              </script>

I think its working...

Hi,

Can you post The html part where checkboxes are created.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.