简体   繁体   中英

JavaScript Validation

I am using the following code to check if at least one checkbox is selected from my checkboxlist in asp.net. I am using jGrowl to throw up a message which works, but the message is still displayed if a selection is made. Any ideas? Also, story_type is an asp label, I am using the code below, but I can't get the jGrowl message to display...

var story_type = document.getElementById('story_type').value;
var agile_list = document.getElementById('agile_factors');
var arrayOfCheckBoxes = agile_list.getElementsByTagName("input"); 

for (counter = 0; counter < arrayOfCheckBoxes.length; counter++) {

    if (arrayOfCheckBoxes[counter].checked) {

        return true;

    } else {

        (function($) {
         $.jGrowl("Please Choose up to 3 Agile Factors", { theme: 'smoke', closer: true });
        })(jQuery);

        return false;
    }

}

if (story_type == "[SELECT TYPE]") {
    (function($) {
        $.jGrowl("Please Select Story Type", { theme: 'smoke', sticky: true, closer: true })
        return false;

    })(jQuery);

    return true;
} 

Since you have jQuery you can do this to simplify your code a bit:

   <div id="options">
        <input type="checkbox" name="opt1" /> Option 1
        <input type="checkbox" name="opt2" /> Option 2
        <input type="checkbox" name="opt3" /> Option 3
        <input type="checkbox" name="opt4" /> Option 4
    </div>
    <div>
        <a href="#" id="validate">validate</a>
    </div>
    <script type="text/javascript">
    ;(function() {

        $("#validate").click(function() {
            validateInputs();
        });


        function validateInputs() {
            alert($("#options input:checked").length + " checked");
        }
    })();

</script>

Here is a jsFiddle of the above code; http://jsfiddle.net/rvXHG/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM