0

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.

1 Answer 1

1

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');
   }
});
Sign up to request clarification or add additional context in comments.

3 Comments

But my requirement is if either one of the field is filled other should be filled
right, so the above code adds a blur event to both name and age, checks to see if the one that was blurred out of has a value, if it does, then it makes that one and the other one required.
Ah, i see what you're saying, updated the answer to check both values before setting/removing the required class

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.