Checkbox Validation

Hello, I’m making a contact form and I have PHP validation so that the user has to have something typed in every category, email validation, etc. But I cannot get the checkbox validated.

My HTML/PHP looks like this:
<div class=“checkmark”>
<label for=“check”>Agree to Terms</label>
<input type=“checkbox” id=“check” name"check" value=“yes”/>
<?php if( isset($errors[‘check’]) ){ ?>
<label for=“check” class=“error”>You must agree to terms</label>
<?php } ?> <br />


How Can I get it so that the user has to click the checkbox in order to agree to terms, and submit the form successfully. If they don’t check the box, I was the error message to show.

Any help would be great!

Thanks a lot.

The validation by PHP is done AFTER the form is sent (server side) by the script the form sends its values to. So in case of errors, you’ll have to send the form and the error messages back to the client (browser).

i ave this where the form sends its values to…

if( empty($check) )
$errors[‘check’] = true;

does anyone know how to prperly validate a checkbox so that the user has to check it off before submitting??

It seems you’re confused on about how things work.

You cannot prohibit someone from pressing “submit” if checkbox isn’t checked only by using PHP.

You can do such things with Javascript. Also, it seems you are working with quite an old example, as it seems that register_globals=on is assumed.

Now, if you want to check whether the checkbox was checked within your php script upon submission, you do it like this:

<div class="checkmark">
<label for="check">Agree to Terms</label>
<input type="checkbox" id="check" name="check" value="yes"/>
<?php if(!isset($_POST['check'])){ ?>
<label for="check" class="error">You must agree to terms</label>
<?php } ?> <br />


You also had a mistake in naming your checkbox. Your code was:

<input type="checkbox" id="check" name"check" value="yes"/>

It should be:

<input type="checkbox" id="check" name="check" value="yes"/>

So try to pay attention to details :slight_smile:

You Said:
You also had a mistake in naming your checkbox. Your code was:
HTML4Strict Code:
<input type=“checkbox” id=“check” name"check" value=“yes”/>

It should be:
HTML4Strict Code:
<input type=“checkbox” id=“check” name=“check” value=“yes”/>

That’s the exact same code.

Also, I tried the PHP code you have below, but the error message shows no matter if you click the checkbox or not and the form cannot be submitted.

Now, how can that be exact same code? Pay close attention to “name” in your example and my example. Don’t just skim trough the code, read it carefully. Code highlighter should help you too.

Naturally, you get the error because your checkbox hasn’t got a NAME. You missed the equal sign when assigning the name. Hence, I even fixed it and pointed it out.

My bad furicane, thanks a lot for your help.

Rookie mistake. It’s all good now.

My code is working great now, but once my page initially loads, the error for my checkbox shows. I want it so that the error messages only show when the checkbox isn’t checked and the user hits the submit button.

Here’s my code…
<label for=“check”>Agree to Terms</label>
<input type=“checkbox” id=“check” name=“check” <?php echo $checked; ?>/>
<?php if(!isset($_POST[‘check’]) ){ ?>
<label for=“check” class=“error”>You must agree to terms</label>
<?php } ?>

Then in my form validator php I got…

$agree = filter_input(INPUT_POST, ‘term’);
$checked = ‘’;

if( empty($agree) )
$errors[‘check’] = true;
$checked = ‘checked’;

Could anyone help me only show the error message when not checked and submitted.?

Thanks a lot!

I think, java script is best suited technique for check box validation because java script works on client machine side server load will be best option is java script.

<script language=“JavaScript” type=“text/JavaScript”>
<!–//hide script
function checkme() {
missinginfo = “”;
if (!document.form.agree.checked) {
missinginfo += "\

  • You must agree to the terms";
    if (missinginfo != “”) {
    missinginfo =“__________________________________
    " +
    “Required information is missing:
    " +
    missinginfo + "
    __________________________________” +
    Please complete and resubmit.”;
    return false;
    else {
    return true;


above code are effective for checkbox validation…