1

I have this validate function:

var validator =$('#form1').validate({
    ignore: "",
    rules: {
        usu_login: { required: true },
        usu_email: { required: true },
        usu_nombre1: { required: true },
        usu_apellido1: { required: true },
        usu_fecha_nac: { required: true },
        usu_cedula: { required: true },
        usu_telefono1: { required: true },
        usu_password: { 
            required: function() { return focusout == true; }
        },
        usu_password2: {
            required: function() { return focusout == true; }
        },
        usu_password3: {
            required: function() { return focusout == true; },
            equalTo: "#usu_password2"
        }
    }
}

I need to apply the same if statement in the equalTo function so this can work as I want to, but I don't know how to do that.

Dows anyone know how? Thanks

7
  • required: function() { return focusout == true; } this acts as an if in the validate plugin @RoryMcCrossan Commented Oct 25, 2013 at 15:55
  • equalTo can only take a string selector. Commented Oct 25, 2013 at 15:57
  • =( so im screwed, well thanks anyway! @RoryMcCrossan Commented Oct 25, 2013 at 15:58
  • So you want equalTo to be applied if focusout is true, and otherwise the equalTo should be ignored? Commented Oct 25, 2013 at 16:00
  • Yes, that is what i want, but i don't know how to do it like in the required rule. @Yogu Commented Oct 25, 2013 at 16:01

3 Answers 3

2

The solution is to use depends property of the rule!!!

jQuery(function ($) {

    var focusout;
    $('#state').change(function(){
        focusout = this.checked;
    }).change();

    var validator = $('#myform').validate({
        ignore: "",
        rules: {
            usu_login: {
                required: true
            },
            usu_email: {
                required: true
            },
            usu_nombre1: {
                required: true
            },
            usu_apellido1: {
                required: true
            },
            usu_fecha_nac: {
                required: true
            },
            usu_cedula: {
                required: true
            },
            usu_telefono1: {
                required: true
            },
            usu_password: {
                required: function () {
                    return focusout == true;
                }
            },
            usu_password2: {
                required: function () {
                    return focusout == true;
                }
            },
            usu_password3: {
                required: function () {
                    return focusout == true;
                },
                equalTo: {
                    depends: function(){
                        return focusout == true;
                    },
                    param: "#usu_password2"
                }
            }
        },
        messages: {}
    });
});

Demo: Fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

can i ask you a question? how would i do the same with the minlenght validation? with the depend fuction too? thanks @ArunPJohny
0

You could try this:

var rules = {
    usu_login: { required: true },
    usu_email: { required: true },
    usu_nombre1: { required: true },
    usu_apellido1: { required: true },
    usu_fecha_nac: { required: true },
    usu_cedula: { required: true },
    usu_telefono1: { required: true },
    usu_password: { 
        required: function() { return focusout == true; }
    },
    usu_password2: {
        required: function() { return focusout == true; }
    },
    usu_password3: {
        required: function() { return focusout == true; },
        equalTo: "#usu_password2"
    }
}

if (!focusout) {
    for (key in rules) {
        delete rules[key].equalTo;
    }
}

var validator = $('#form1').validate({
    ignore: "",
    rules: rules
});

The loop iterates over all rules and removes the equalTo constraint if it should be ignored.

However, you have to recreate the validator object every time you change focusout.

2 Comments

I tried it, thank you, but it doesn't work, because that field usu_password3 is inside a modal, and even if its equealTo NULL, its expecting the input to appear and it hasnt appeared yet becase its inside the modal.
I changed it to delete, which gets rid of the equalTo property entirely, as if it was never present. BUt Aurun's answer is the better one.
0

in your case you have to add your own method to validator

$.validator.addMethod("mehod", function(value, element) 
{
valid=false;
if(condition==true)//place your  condition
{
valid=true;
}
else
{
valid=false;
}
return this.optional(element) || valid;
},"Your own error message");

and place it where you want to validate

like this:

rules: {
usu_login: { required: true,method: true },//here you are using your method
usu_email: { required: true }
/*other rule*/
}

This works for my case.

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.