Link to home
Start Free TrialLog in
Avatar of ucohockey
ucohockey

asked on

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

Avatar of chaau
chaau
Flag of Australia image

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

ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial