views:

41

answers:

1

Huy Guys,

I am trying to use jquery validate and specify that one field is required only when 3 other fields are blank.

the thing is that the rule is working but it still remain requiring the field even when I type value on it.

Thanks.

This is my code:

$("#ProspectDtlFrm").validate({
 rules: {

  prsptEmail: {
   required: function(element) { return ( $("#prsptHomePhone").val() == '' && $("#prsptBusinessPhone").val() == '' && $("#prsptMobilePhone").val() =='') }                                         
   }    
 },
 messages: {
      prsptEmail: "Please enter your first name"
 }
}); 
+1  A: 

You could use depends:

$('#ProspectDtlFrm').validate({ 
    rules: {
        prsptEmail: {
            required: {
                depends: function(element) {
                    return ($('#prsptHomePhone').val() == '' && 
                            $('#prsptBusinessPhone').val() == '' && 
                            $('#prsptMobilePhone').val() == '');
                }
            }
        }    
    }, 
    messages: { 
        prsptEmail: 'Please enter your first name'
    } 
});
Darin Dimitrov
Hi, It remains doing the same. when I enter values in the field it still keep requiring.
cesar
Let me give you more details about what I am trying to do. I have 4 fields where the user should enter values in at least one of them. So I am trying to validate that each of these 4 fields will be required just when all of them are empty otherwise it won't be required.
cesar
Sorry, I made a mistake in my initial answer. Please see my update. The `depends` function should be nested inside the `required` condition. Now if you type something into either `prsptHomePhone`, `prsptBusinessPhone` or `prsptMobilePhone`, the `prsptEmail` field should no longer be required.
Darin Dimitrov
Hi Darin, it works when I type something into either prsptHomePhone, prsptBusinessPhone or prsptMobilePhone, but when I type something into the field Email it is still required.
cesar