form validation

I have a form that I'm trying to validate on click of button. I have a password and a checkbox and would like to display a java alert when they are not selected. For example if the passcode is not field in I want to display a java alert saying please fill enter passcode, and then if the checkbox is not checked I want a java alert to say please check the box.

<script type="text/javascript">
        $(document).ready(function() {
            $('.connectButton').click(function(){
				if($('#check1').is(':checked')){
                                      return true;
                              } else{
                                      swal("Oops...", "Please review and then click the Terms and Conditions check box!", "error");
                                      return false;
                              }
                        if ($('.pcodeHome').val() === '') {
        alert("Oops...", "Please enter passcode!", "error");
        return false;
    } 
return: true;
}
                $('.connectButton').fadeOut(function(){
                    $('.package-wrapper').hide();
                    $('#progress').fadeIn();                    
                });
                                
            });
        });    
    </script>

Open in new window

ucohockeyAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaauCommented:
I think the problem is here:
if($('#check1').is(':checked')){
                                      return true;

Open in new window

As you can see, if the checkbox is checked you return from the function and skin further validation. What you need to do instead is to check if the checkbox is not checked but continue to validate the rest, like this:
<script type="text/javascript">
        $(document).ready(function() {
            $('.connectButton').click(function(){
				if(!$('#check1').is(':checked')){
                                      swal("Oops...", "Please review and then click the Terms and Conditions check box!", "error");
                                      return false;
                              }
                        if ($('.pcodeHome').val() === '') {
        alert("Oops...", "Please enter passcode!", "error");
        return false;
    } 
return: true;
}
                $('.connectButton').fadeOut(function(){
                    $('.package-wrapper').hide();
                    $('#progress').fadeIn();                    
                });
                                
            });
        });    
    </script>

Open in new window

Michel PlungjanIT ExpertCommented:
How does the form look? If .connectButton is a type=submit, I strongly advise changing the code to the following, which also fixes some brackets and the use of return as a label
$(function () {
    $('form').on("submit", function (e) {
        if (!$('#check1').is(':checked')) {
            swal("Oops...", "Please review and then click the Terms and Conditions check box!", "error");
            e.preventDefault();
        } else if ($('.pcodeHome').val() === '') {
            alert("Oops...", "Please enter passcode!", "error");
            e.preventDefault();
        } else {
            $('.connectButton').fadeOut(function () {
                $('.package-wrapper').hide();
                $('#progress').fadeIn();
            });
        }
    });
});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.