user3233074
user3233074

Reputation: 529

jquery submit validation not working

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?

Upvotes: 0

Views: 33

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

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();
    });
});

Upvotes: 1

Related Questions