0

js

 function checkgender(){
       $("#gender-result").hide();
          if (undefined == $("input[name='gender']:checked", "#contact-form").val()) {
               $("#gender-result").html('gender requirements.').show();
               $("#gender-result").css('color', 'red');
               return false;          
          }else{
                $("#gender-result").hide();
          }
          return true;
     }

function checkpassword(){
        var password_value = $("#reg-password").val();

        if(password_value === ""){
            //password emtpy
            $("#password-result").html('Please make a password');
            $("#password-result").css('color', 'red');
            $('#reg-password').css('border-color', 'red');
        }else if (password_value.length < 6){
            //password is too shot 6 digi
            $("#password-result").html('Your password is too short.');
            $("#password-result").css('color', 'red');
            $('#reg-password').css('border-color', 'red');
        }else{
            //password passed
            $("#password-result").html('');
            $('#reg-password').css('border-color', '#dfe0e6');
            return true;
        }
        return false;
    }

but why it wont work if i use this to submit validation

   $(document).click(function() {
            $('#contact-form').submit(function(){
                event.preventDefault();
                //validation
                if(!checkgender() && !checkpassword()){
                return false;
                }else{
                return true;
                }
                });
            });

if i submit one of the function it work properly , but if i put 2 together and press submit , it let me passed the validation even one of it is empty , why ? what i done wrong here?

0

1 Answer 1

1

You need to return false if either one of the conditions is falied so

$(document).click(function () {
    $('#contact-form').submit(function () {
        //validation
        if (!checkgender() || !checkpassword()) {
            return false;
        } else {
            return true;
        }
    });
});

or

shorter

$(document).click(function () {
    $('#contact-form').submit(function () {
        //validation
        return checkgender() && checkpassword();
    });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.