Require fieldset after checkbox checked

断了今生、忘了曾经 提交于 2019-12-06 10:44:52

Client-side validation is always going to fall second-best to server-side validation, as the user can always disable Javascript or force-post nonsense that can override browser ability, but your server can't be fooled as easily! That said, you're on the right track, although I'd be tempted to fix it up a little...first, your HTML (abridged):

<form method="post">
    <input id="ship_to_billing" type="checkbox" name="properties" />
    <div class="fieldgroup" id="shipping_info">
        <input type="text" id="shipping_name" name="properties[Recipient]" />
        <input type="text" id="address_street" name="properties[Address]" />
        ...
    </div>
    <input type="submit" onclick="return validateMyStuff();" value="Submit" />
</form>

Next, your script, slightly more robust:

<script src="/path/to/jquery.js"></script>
<script type="text/javascript">

    // this will run when the document's done loading...
    $(function(){
        $("#ship_to_billing").change(function(){
            // make sure of the checked state...
            // hide the fields if the checkbox is checked...
            // 'this' is the checkbox...
            if ($(this).is(":checked")) // fixed error here! ;)
                $("#shipping_info").hide();
            else
                $("#shipping_info").show();
        });
    });

    // validate the input, only if the checkbox is not checked...
    // this will be called when your submit button is clicked...
    function validateMyStuff() {
        if (!$("#ship_to_billing").is(":checked")) {
            // check to see that the other input fields have values...
            var inputs = $("#shipping_info input");
            var ready = true;
            inputs.each(function(){
                // 'this' is the current input we're validating...
                if ($(this).val() == '') {
                    ready = false;
                    return false; // exits jQuery '.each' function...
                }
            });
            if (!ready) {
                alert("You forgot to fill in something!");
                return false;
            }
        }
        return true;
    }

</script>

Well, that's the best interpretation I can give based on the limited information in the original question. Hope this helps! :)

EDIT 1:

My apologies! I left out a closing bracket ) on line 10 of my JavaScript code! I've fixed the problem, and copy / pasting the code above into an HTML page seems to work fine. Remember to include your jQuery script above the rest of your scripts, and to put your scripts in the <head>...</head> section of your HTML.

As far as client-side validation, you could give all the inputs inside the shipping_info fieldset the required attribute like this

$('#shipping_info input').attr('required', true);

But this doesn't guarantee the fields will be filled, as the user can bypass this. So you have to check it server-side whatever you do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!