AJJ
AJJ

Reputation: 897

Checkbox validation not working

I have some javascript that is supposed to validate a checkbox (making it mandatory) but my form regardless of whether it is checked or not just submits, here is my code :

<script>
  function validateCheckBoxes(theForm) {
    if (!theForm.declare.checked) {
      alert ('You must tick the checkbox to confirm the declaration');
      return false;
    } else {    
      return true;
    }
  }
</script>

<form name="form" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return validateCheckBoxes(this);">
  <input type="checkbox" name="declare" id="declare">
  <input type="submit" name="submit" id="submit" value="submit">
</form>

Any ideas on why it isnt working?

Upvotes: 0

Views: 1696

Answers (4)

me_digvijay
me_digvijay

Reputation: 5502

Here is a what I tried and worked for me

<script type='text/javascript'>
    function validateCheckBoxes() 
{
if (document.forms[0].declare.checked == false)
{
    alert ('You must tick the checkbox to confirm the declaration');
    return false;
} else {    
    return true;
}
}
</script>

<form name="form" method="POST" action="yourURL/search" id="eoi"  onsubmit="return validateCheckBoxes();">
    <input type="checkbox" name="declare" id="declare"/>
</form>

I don't think you need to pass the reference of the form object to the function.

Upvotes: 0

Sudhir Kumar
Sudhir Kumar

Reputation: 1

 <script>
  function validateCheckBoxes() 
  {
    if (document.getElementById('declare').checked == false)
     {
      alert ('You must tick the checkbox to confirm the declaration');
      return false;
     } else {    
     return true;
     }
  }
</script>

<form name="form" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi"        onsubmit="return validateCheckBoxes();">

<input type="checkbox" name="declare" id="declare" />
<input type="submit" name="submit" value="submit" />

</form>

Upvotes: 0

asifsid88
asifsid88

Reputation: 4701

<script type="text/javascript">
function validateCheckBoxes(theForm) 
{
if (!theForm.declare.checked)
{
    alert ('You must tick the checkbox to confirm the declaration');
    return false;
} else {    
    return true;
}
}
</script>

<form name="form" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return validateCheckBoxes(this);">

<input type="checkbox" name="declare" id="declare">

</form>

As @shin mentioned, you need to specify the object of the form during the function call.
theForm.declare.checked returns true or false there is no need to check the value with == operator. Just use it directly (User ! depending on need - to negate the result)

Upvotes: 1

Shijin TR
Shijin TR

Reputation: 7768

change your function call like

         return validateCheckBoxes(this);

Eg:

      <form name="form" method="POST" action="<?php echo $_SERVER["PHP_SELF"];?>" id="eoi" onsubmit="return validateCheckBoxes(this);">

Because theForm should refer an object

Upvotes: 0

Related Questions