表单验证所有验证都有效但复选框验证不起作用

时间:2014-01-05 09:23:53

标签: javascript jquery html forms

function validateForm()

{

var x=document.forms["myForm"]["email"].value;


if (x==null || x=="")

  {


  alert("Email must be filled out");

  return false;

  }


  var a=document.forms["myForm"]["password"].value;


if (a==null || a=="")


  {


  alert("Password must be filled out");



  return false;


  }


  var b=document.forms["myForm"]["name"].value;


if (b==null || b=="")


  {



  alert("Name must be filled out");


  return false;


  }


   var c=document.forms["myForm"]["phone"].value;


if (c==null || c=="")


  {


  alert("phone must be filled out");


  return false;


  }


   var d=document.forms["myForm"]["month"].value;



if (d==null || d=="")


  {

  alert("month must be filled out");

  return false;

  }


   var e=document.forms["myForm"]["day"].value;


if (e==null || e=="")


  {


  alert("day must be filled out");


  return false;


  }


   var f=document.forms["myForm"]["year"].value;


if (f==null || f=="")


  {


  alert("year must be filled out");


  return false;


  }


  var g=document.forms["myForm"]["year"].value;


if (g==null || g=="")

  {

  alert("year must be filled out");

  return false;

  }

  var h=document.forms["myForm"]["add1"].value;

if (h==null || h=="")
  {

  alert("Address must be filled out");

  return false;

  }

   var i=document.forms["myForm"]["city"].value;
if (i==null || i=="")

  {


  alert("city must be filled out");

  return false;

  }

   var j=document.forms["myForm"]["pcode"].value;

if (j==null || j=="")

  {


  alert("post code must be filled out");


  return false;


  }


  if(!this.form.checkbox.checked)


    {

        alert('You must agree to the terms First.');

        return false

        }

        if(!this.form.agree.checked)
    {
        alert('You must agree to the terms Secend.');

        return true;

        }
        if(!this.form.under.checked)

    {
        alert('You must agree to the terms Third.');

        return true;
        }

}

<form action="register.php" method="post" id="signupfrm" name="myForm" onsubmit="return validateForm()" >

 <input type="checkbox" name="agree" id="agree" / style="margin-left:65px;">


 <input type="checkbox" name="under" id="under" value="1" / style="margin-left:65px;">


 <input type="checkbox" name="checkbox" id="dec" value="1" / style="margin-left:65px;"> 



<input type="submit" name="submit" value="submit"   / style="margin-left:500px; width:120px; height:35px;" />


</form>

2 个答案:

答案 0 :(得分:0)

这是来自不同来源的复制/粘贴代码......无处不在,您使用

document.forms["myForm"]["XXX"].value;

除了有缺陷的部分:

if(!this.form.checkbox.checked)

所以只需自己写一下......

答案 1 :(得分:0)

你最好使用:

$("#agree").is(":checked");