Simple JavaScript Checkbox Validation

前端 未结 11 1992
长情又很酷
长情又很酷 2020-12-10 03:56

I usually work with PHP so sadly don\'t have some basic JS principles down. This is all I want to accomplish--I\'ve seen many posts on this topic but they are usually beyon

相关标签:
11条回答
  • 2020-12-10 04:25

    If the check box's ID "Delete" then for the "onclick" event of the submit button the javascript function can be as follows:

    html:
    <input type="checkbox" name="Delete" value="Delete" id="Delete"></td>
    <input type="button" value="Delete" name="delBtn" id="delBtn" onclick="deleteData()">
    
    script:
    <script type="text/Javascript">
        function deleteData() {
            if(!document.getElementById('Delete').checked){
                alert('Checkbox not checked');
                return false;
            }
    </script>
    
    0 讨论(0)
  • 2020-12-10 04:26

    You can do something like this:

    <form action="../" onsubmit="return checkCheckBoxes(this);">
        <p><input type="CHECKBOX" name="MyCheckbox" value="This..."> This...</p>
        <p><input type="SUBMIT" value="Submit!"></p>
    </form>
    
    <script type="text/javascript" language="JavaScript">
    <!--
    function checkCheckBoxes(theForm) {
        if (
        theForm.MyCheckbox.checked == false) 
        {
            alert ('You didn\'t choose any of the checkboxes!');
            return false;
        } else {    
            return true;
        }
    }
    //-->
    </script> 
    

    http://lab.artlung.com/validate-checkbox/

    Although less legible imho, this can be done without a separate function definition like this:

    <form action="../" onsubmit="if (this.MyCheckbox.checked == false) { alert ('You didn\'t choose any of the checkboxes!'); return false; } else { return true; }">
        <p><input type="CHECKBOX" name="MyCheckbox" value="This..."> This...</p>
        <p><input type="SUBMIT" value="Submit!"></p>
    </form>
    
    0 讨论(0)
  • 2020-12-10 04:27
    var testCheckbox = document.getElementById("checkbox");  
    if (!testCheckbox.checked) {
      alert("Error Message!!");
    }
    else {
      alert("Success Message!!");
    }
    
    0 讨论(0)
  • 2020-12-10 04:30

    Guys you can do this kind of validation very easily. Just you have to track the id or name of the checkboxes. you can do it statically or dynamically.

    For statically you can use hard coded id of the checkboxes and for dynamically you can use the name of the field as an array and create a loop.

    Please check the below link. You will get my point very easily.

    http://expertsdiscussion.com/checkbox-validation-using-javascript-t29.html

    Thanks

    0 讨论(0)
  • 2020-12-10 04:32

    Another simple way is to create a function and check if the checkbox(es) are checked or not, and disable a button that way using jQuery.

    HTML:

    <input type="checkbox" id="myCheckbox" />
    <input type="submit" id="myButton" />
    

    JavaScript:

    var alterDisabledState = function () {
    
    var isMyCheckboxChecked = $('#myCheckbox').is(':checked');
    
         if (isMyCheckboxChecked) {
         $('myButton').removeAttr("disabled");
         } 
         else {
                 $('myButton').attr("disabled", "disabled");
         }
    }
    

    Now you have a button that is disabled until they select the checkbox, and now you have a better user experience. I would make sure that you still do the server side validation though.

    0 讨论(0)
提交回复
热议问题