jquery Validate - Custom validation at runtime
In my form i have two text boxes txtName & txtAge . I use jquery.validate.js to make it as required field validator and it is working fine. Now my requirement is sometimes based on some business logic i need to make these 2 fields non-mandatory programatically. But if user fills txtName he needs to fill txtAge too or Vice Versa.
开发者_开发百科If either one of the field is filled other should be filled, Or both can be left empty
I'm using asp.net in server side.
if you are using the css class based validation, then you should be able to do this by simply removing/adding the "required" class in the inputs as needed.
$('#txtName, #txtAge').blur(function(){
nameElem = $('#txtName);
ageElem = $('#txtAge');
if(nameElem.val() != "" || ageElem.val() != "")
{
$('#txtAge,#txtName').addClass('required');
}else{
$('#txtAge, #txtName').removeClass('required');
}
});
精彩评论