如何写jquery中的rules标签中的条件

时间:2014-03-22 10:10:21

标签: jquery

如果jquery规则中的条件标记,任何身体都可以帮助我努力写作 看下面的标签..

    $("#submit-btn-id").on("click",function(){
        var formValid = getValidator("#userRegistration", {
            rules : {
                firstname : "required",
                lastname: "required",
                password:"required",
                gender:"required",
                email:{
                    required:true,
                    email:true
                },
                phone:"required",
                //select:"required",
                select:{
                    required : true,
                    selectNone: true
                    },
  if(("#selectid option:selected").val() == '3'){
                        dateOfJoining:"required",
                        idCardNumber:"required",
}
    else if(("#selectid option:selected").val() == '3'){
                        code:"required",
                        creditLimit:"required",
                        depositAmont:"required",
                        idProofNumber:"required",
                        identyProof:"required",
                        "panNumber":{panNumber:true},
    }
   else  if(("#selectid option:selected").val() == '4'){
                        AgentApicreditLimit:"required",
                        AgentApidepositAmont:"required",
                        AgentApinoOfIp:"required",
    }
                        "AgentApipanNumber":{AgentApipanNumber:true}
            }
    });

1 个答案:

答案 0 :(得分:0)

从JQuery中,您可以添加一个函数来将您的逻辑作为属性值。

方法1: 添加所有验证属性,一些将具有真值,一些将取决于您的逻辑的假值。

以下是jQuery Validation Plugin的一个例子。

$(".selector").validate({
  rules: {
    contact: {
      required: true,
      email: {
        //Note here how they placed some logic into the function
        depends: function(element) {
        return $("#contactform_email:checked")
      }
     }
    }
   }
  });

要与您的代码相关联,您可以执行以下操作:

...
   code: function (){
                           //your logic here. 
                           //DON'T FORGET THE $ 
                          return $("#selectid option:selected").val()==3                          
                       },

   creditLimit: function(){return $("#selectid option:selected").val() == '3'},
    ...

   dateOfJoining: = function ()
   {
      //your logic goes here 
      if(("#selectid option:selected").val() == '3')
      {
      return "required"; //this will be the value of  dateOfJoining
      }
   }

方法2:

正如阿切尔在评论中所建议的那样。您可以在致电getValidator之前构建getValidator对象。此外,getValidator需要一个对象作为第二个参数。声明该对象的位置并不重要。因此,您可以在致电getValidator

之前准备该对象

示例:

$("#submit-btn-id").on("click",function(){
var valObject = {};//this will be the rules object

    if(("#selectid option:selected").val() == '3'){
                       valObject ["dateOfJoining"] = "required" ;//we add attributes based on the logic
                       valObject ["idCardNumber"] = "required" ;// we add more 

attributes           
//here add more attributes as if they satisfy the condition. 
         }

//Add more conditions, more constrain as above to add more required attributes. 
   var rules = {"rules" : valObject } ; //assign all attributes that you've already defined to the rules object. 
//And then call your validation
var formValid = getValidator("#userRegistration", rules );
};

两种方式都很好。您只需要为您选择更简单的方法。