user1673394
user1673394

Reputation: 617

How to validate the hyper link and checkbox when submit button is clicked

we have a submit button with a checkbox and a hyper link in a view in mvc 3. we need validate both the check box and the hyper link on the click of the submit button. For eaxmple in case the checkbox is not checked and the user hits the submit we need to show the error message "please check the checkbox". in case if the checkbox is checked and the hyper link is not clicked by the user we need to show the error message "Please click the link". how to solve this senario using jquery and mvc 3?

Upvotes: 0

Views: 596

Answers (1)

von v.
von v.

Reputation: 17108

You need to have a way to keep track of the clicked action for the hyperlink on the client side. If you wish to keep track of it on the server then that's another thing. So suppose your hyperlink has an id Link1 and the checkbox has an id AcceptMe. You can do something like

<script>
    var isLinkClicked = false;
    $(function() {
        $("Link1").click(function() {
            isLinkClicked = true;
        });
        $("#submitButtonId").click(function(){
            if (!isLinkClicked) { 
               alert("Please click the link"); // or show it somewhere
               return;
            }
            if (!$("#AcceptMe").is(":checked")) {
               alert("Please check the checkbox"); // or show it somewhere
               return;
            }
            // else submit your form
        });
    });
</script>

Upvotes: 1

Related Questions