jQuery.Validate Conditional Validation RadioButton and DropDownList

前端 未结 3 1219
醉酒成梦
醉酒成梦 2020-12-06 02:56

I am trying to set up some validation, so that if the \'Yes\' radio button is checked another control, which is a drop down list needs to be validated to ensure it is not se

相关标签:
3条回答
  • 2020-12-06 03:33

    This doesn't work with current version of Validate.js (v 1.7)

    I was also able to prove the very concept doesn't work:

    rules: { 
      xyz: { 
        required: function(element) { 
          return true; 
        } 
      }                 
    } 
    

    This also fails.

    0 讨论(0)
  • 2020-12-06 03:40

    As long as the first drop-down list option has a blank value (<option value="">some text</option>), the following rule will work:

    rules: {
      NCBYears: {
        required: function(element) {
          return $("input:radio[name='PreviousInsurance']:checked").val() == 'yes';
        }
      }                
    }
    

    from: http://docs.jquery.com/Plugins/Validation/Methods/required#dependency-expression

    0 讨论(0)
  • 2020-12-06 03:44

    ScottE is correct. I have a dropdownlist populated from a database and I just appended an item at the very top of the list and gave it a value of "". When you validate the input just validate against the dropdownlist being blank. You could also hard code the list if it never changes and add an item to the top with a blank value, example below.

    _example.Add(new SelectListItem { Text = "Select Example", Value = ""});
    
    0 讨论(0)
提交回复
热议问题