Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery validate when field not empty

I validate my form using jquery validate like this:

$("#formuser").validate({

        rules: {

           accountname: {
            required: true,
            minlength: 6
        },
        T1: {
            required: true,
            minlength: 6
        },
            accountpassword1: {
                required: true,
                minlength: 8
            },
            accountpassword2: {
                required: true,
                minlength: 8,
                equalTo: "#accountpassword1"
            },
        },
 });

HTML:

<input id="accountpassword1" class="text-input form-control"  type="text" name="accountpassword1" size="24" placeholder="password" >

<input class="text-input form-control"  type="text" name="accountpassword2" size="24" placeholder="password" >

this worked for me But. I need to check jquery validate when password1 and password 2 not empty. my mean is if password input is empty jquery validate not validate field else validate field password1 and password2.

how do create this?

NOTE:: i need to only check validate accountpassword1 and accountpassword1 with this method.

like image 388
Pink Code Avatar asked Oct 04 '14 12:10

Pink Code


2 Answers

You can use the depends option like

function isPasswordPresent() {
    return $('#accountpassword1').val().length > 0;
}

$("#formuser").validate({
    rules: {
        accountname: {
            required: true,
            minlength: 6
        },
        T1: {
            required: true,
            minlength: 6
        },
        accountpassword1: {
            //required: true is not required
            minlength: {
                depends: isPasswordPresent,
                param: 8
            }
        },
        accountpassword2: {
            required: isPasswordPresent,
            minlength: {
                depends: isPasswordPresent,
                param: 8
            },
            equalTo: {
                depends: isPasswordPresent,
                param: "#accountpassword1"
            }
        },
    },
});

Demo: Fiddle

like image 112
Arun P Johny Avatar answered Oct 17 '22 01:10

Arun P Johny


I had the same requirement So I used this code

 $.validator.addMethod(
    "regex",
    function(value, element, regexp){        
        return this.optional(element) || regexp.test(value);
    },
    "Please check your input.");


$('#registration').validate({        
        rules: {
        first_name: {
            required: true , regex: /([-a-zA-Z0-9]|\0)/                
        },
        last_name: {
            required: true , regex: /([-a-zA-Z0-9]|\0)/
        }            
    },        
    errorPlacement: function(){
       return false;
    },        
    submitHandler: function (form){
        alert('sucess')
        return false;
    }
}); 

its working for me. Hope it helps

like image 34
Anoop Saini Avatar answered Oct 16 '22 23:10

Anoop Saini