如何在js中使用验证器验证下拉列表

How to validate dropdown list using validator in js

本文关键字:验证 下拉列表 js      更新时间:2023-09-26

我正在尝试验证js中的下拉列表。这是我的脚本代码

$.validator.addMethod(
  'drop_down_validation',
  function (value, element) {
    alert ("my_fun")
    var val = $('#creative_offer_type').val();
    alert (val);
    if (value.length==0 && val=="") {
       return false;
    }
       else return true;
  },
  $.format("must select atleast one value")
);

var form_rules = {
    'creative[offer_type]' : {
                              required: true,
                              drop_down_validation: true
                            },
  };
  var form_messages = {
    'creative_offer_type' : { required: 'You must specify Offer Type'},
  };

是正确的吗?我试过这样做,但在UI中没有显示任何响应。

供您参考

function JSFunctionValidate()
{
if(document.getElementById('<%=ddlView.ClientID%>').selectedIndex == 0)
{
alert("Please select ddl");
return false;
}
return true;
}

只需在下拉列表中调用此方法即可。。