Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I validate in bootstrap that at least one checkbox is marked and pass this information to php?

I'm using bootstrap and I created a form with 8 checkboxes. When user clicks "Submit" I want to verify if at least one checkbox is marked and if not - let user know with an error message below the checkbox group. Also, I would like to verify if the date that he puts is correct with my default data-form: 23/09/2015 05:45 PM

This is my java-script validation code:

$('#myform').validate({// initialize the plugin
    errorElement: 'div',
    rules: {
        datetimepicker: {
            required: true,
            date: true
        },
        commercialText: {
            required: true,
            minlength: 5
        },
        terms: {
            required: true,
            maxlength: 2
        }
    },
    submitHandler: function (form) { 
        var text = $("#customtext").val();
        var date = $("#datetimepicker").val();
        var stand = 2; 
        $.ajax({
            url: 'savedatanow.php',
            type: "POST",
            data: {
                text: text,
                date: date,
                stand: stand

            },
            dataType:'text',
            success: function(response)
            {

                alert(response);
            }
        });
        //alert('outside ajax');
    },
    highlight: function (element) {
        $(element).closest('.form-group').removeClass('success').addClass('has-error');
    },
    success: function (element) {
        element.addClass('valid')
            .closest('.form-group').removeClass('error').addClass('has-success');
    }                   
});

I prepared the following fiddle: http://jsfiddle.net/5WMff/1316/ but I have a problem with marking the correct error messages in red. When user doesn't check that he accepts the terms there's a weird situation happening too (the whole text is marked in red, not only the error)... Can you help me with that? Thanks!

like image 346
randomuser1 Avatar asked Sep 20 '15 15:09

randomuser1


People also ask

How do I select a checkbox in atleast 1?

There is a form with multiple checkboxes and we're going to make sure that at least one is checked using pure JavaScript. To set a custom validation error message, we will use setCustomValidity() method.

How do you validate a form with multiple checkboxes to have atleast one checked?

jQuery. validator. addMethod("validatorName", function(value, element) { if (($('input:checkbox[name=chkBox1]:checked'). val() == "Val1") || ($('input:checkbox[name=chkBox2]:checked').

How can I require at least one checkbox be checked before a form can be submitted?

How can I enforce that requirement? You could use PHP to check if at least 1 of the check boxes is checked. You would probably also want to make sure your <input "name"> is different for each check box otherwise getting the return variable values might be tricky.

How do you validate a check box?

Checking if a checkbox is checked First, select the checkbox using a DOM method such as getElementById() or querySelector() . Then, access the checked property of the checkbox element. If its checked property is true , then the checkbox is checked; otherwise, it is not.


1 Answers

Create custom validator for group of elements.

$.validator.addMethod( "at_least_one", function() {
    return $( "input[name='number[]']:checked" ).length;
}, 'Select at least one number' );

Add rule.

rules: {
    "number[]": "at_least_one",
    .....

Add name and value attributes to checkboxes, here is an example of how to creates an array of the selected values with square braces to the checkbox names. That way $_POST['number'] will be an array of checked values.

<input type="checkbox" value="two" name="number[]">

UPDATED FIDDLE

For dates you can use jQuery Datepicker or THIS SO question, basically create another custom validation method.

Now, there is still some issues with error messages and bootstrap, I hope that someone else will help you with that.

like image 71
Danijel Avatar answered Oct 15 '22 03:10

Danijel