Jump to content

[SOLVED] validate a check box


ainoy31

Recommended Posts

i have a form that requires to either check a yes or no box.  i need to validate that a box has been checked.  here is what i have but not working.

function check()

{

if(document.flight_ticket.yn.checked == false)

{

alert ("Please check YES or NO to confirm flight");

return false;

}

return true;

}

 

much ty.

 

Ainoy

Link to comment
Share on other sites

Well this is completely a javascript issue, but, i've no idea what the problem is. Your javascript code is fine. I can only assum its to do with the way you call it. Try:

 

<html>
<head>
<script type="text/javascript">
function check()
{
if(document.flight_ticket.yn.checked == false) 
{
  alert ("Please check YES or NO to confirm flight");
  return false;
}else{ 
return true;
}
}
</script>
</head>
<body>
<form name="flight_ticket" onSubmit="return check()">
<input type="checkbox" name="yn" /><br />
<input type="submit" name="submit"  />
</form>
</body>
</html>

 

EDIT:

 

How exactly can you check yes or check no for a single check box?

 

Good point. I was assuming the idea was to see if the checkbox had been clicked, even if the error message is somewhat strange.

Link to comment
Share on other sites

Just a correction to you're script ben:

<html>
<head>
<script type="text/javascript">
function check()
{
if(document.flight_ticket.yn.checked == false) 
{
   alert ("Please check YES or NO to confirm flight");
   return false;
}else{ 
return true;
}
}
</script>
</head>
<body>
<form name="flight_ticket" onsubmit="check()">
<input type="checkbox" name="yn" /><br />
<input type="submit" name="submit"  />
</form>
</body>
</html>

 

Link to comment
Share on other sites

You should not use checkboxes. Checkboxes are meant for independant options that are either true or false. Plus you have given them the same name, which can complicate things as well.

 

You should use a radio group instead which will only allow one of the options to be selected:

 

HTML

Please confirm:
<input type="radio" name = "yn" value="NO"> NO
<input type="radio" name = "yn" value="YES"> YES

 

JavaScript

function check() {

  ynfield = document.flight_ticket.yn;

  if (!ynfield.options[0].checked && !ynfield.options[1].checked) {
   alert ("Please select YES or NO to confirm flight");
   return false;
  }

  return true;
}

Link to comment
Share on other sites

I misreferenced the radio buttons:

 

function check() {

  ynfield = document.flight_ticket.yn;

  if (!ynfield[0].checked && !ynfield[1].checked) {
   alert ("Please select YES or NO to confirm flight");
   return false;
  }

  return true;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.