Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 剑道ui网格-使用自定义添加/编辑弹出表单模板时,同一自定义验证规则不适用于多个字段_Jquery_Validation_Kendo Ui_Customvalidator_Kendo Validator - Fatal编程技术网

Jquery 剑道ui网格-使用自定义添加/编辑弹出表单模板时,同一自定义验证规则不适用于多个字段

Jquery 剑道ui网格-使用自定义添加/编辑弹出表单模板时,同一自定义验证规则不适用于多个字段,jquery,validation,kendo-ui,customvalidator,kendo-validator,Jquery,Validation,Kendo Ui,Customvalidator,Kendo Validator,我正在使用网格添加/编辑弹出表单的自定义模板 这是我的名片 在我的弹出表单中,我有两个邮政编码字段,分别命名为postcode1和postcode2,这两个字段都需要使用自定义验证器规则进行验证 现在的问题是,如果我在两个字段上应用相同的自定义验证规则postalCode,那么验证只对输入字段postcode2有效,而对输入字段postcode1的验证停止工作。 但是,对于输入字段postcode2,如果我将自定义验证规则名称从postalCode更改为postalCode2,则验证将开始对这两

我正在使用网格添加/编辑弹出表单的自定义模板

这是我的名片

在我的弹出表单中,我有两个邮政编码字段,分别命名为
postcode1
postcode2
,这两个字段都需要使用自定义验证器规则进行验证

现在的问题是,如果我在两个字段上应用相同的自定义验证规则
postalCode
,那么验证只对输入字段
postcode2
有效,而对输入字段
postcode1
的验证停止工作。

但是,对于输入字段
postcode2
,如果我将自定义验证规则名称从
postalCode
更改为
postalCode2
,则验证将开始对这两个字段进行

由此我了解到,在多字段上使用相同的自定义验证规则会导致问题

那么,有人知道如何创建可应用于多个字段的自定义验证规则吗

任何帮助都将不胜感激!谢谢

下面是我的js fiddle演示中的代码:

HTML:

<div id="grid"></div>

<script type="text/html" id="popup_editor_template">
    <div>
    <h4>In Post Code fields, enter value length less than or greater than 4 to see custom validation error message for postcode</h4>
    Address 1 : <input type="text" name="address1" required/> <br/>
    Post code 1 : <input type="number" name="postcode1" required                                                data-postalCode-msg="Postal Code must be four digits"
/> <br/><br/>
    Address 2 : <input type="text" name="address2" required/> </br>
    Post code 2 : <input type="number" name="postcode2" required
                                        data-postalCode-msg="Postal Code must be four digits"
                    /> <br/>
  </div>
</script>
function validatePostalCode(input)
{
    //if ( input.is("[data-customPostalCode]") && input.val() )
  if (input.val() )
  {
    console.log("in validatePostalCode: ", input.attr('name'), input.val(), input.val().length);

    //if (input.val().length != 4)
    if( input.val().length != 4 || ( /\D/.test(input.val()) ) )
      return false;
  }
  return true;
}

$("#grid").kendoGrid({
    dataSource: {
        type: "odata",
        transport: {
            read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
        },       
        pageSize: 10,
        serverPaging: true,
        schema: {
            model: {
            fields: {
              postcode1: {
                type: "number",
                defaultValue: null,
                validation: {
                  postalCode: function (input) {
                    console.log('in heree');
                    if (input.is("[name=postcode1]"))
                    {
                      return validatePostalCode(input);
                    }
                    return true;
                  }
                }
              },

              postcode2: {
                type: "number",
                defaultValue: null,
                validation: {
                    //when changing rule name from "postalCode" to "postalCode2", the validation starts working on both fields
                  postalCode: function (input) {
                    console.log('in heree toooo');
                    if (input.is("[name=postcode2]"))
                    {
                      return validatePostalCode(input);
                    }
                    return true;
                  }
                }
              }
            }
        }
    },
    },
    height: 800,    
    pageable: true,
    columns: [
        "OrderID",
        "Freight",        
        "ShipName",                            
        "ShipCity"        
    ],
    toolbar: [
      { name: "create" },
    ],
    editable : {
      mode: 'popup',
      template: kendo.template($('#popup_editor_template').html())
    },
    save: function(e) {
        alert('Now save');
      e.preventDefault();
    }

});
var validator;

function validatePostalCode(input)
{
  if (input.val() )
  {
    //console.log("in validatePostalCode: ", input.attr('name'), input.val(), input.val().length);

    //if (input.val().length != 4)
    if( input.val().length != 4 || ( /\D/.test(input.val()) ) )
      return false;
  }
  return true;
}

$("#grid").kendoGrid({
    dataSource: {
        type: "odata",
        transport: {
            read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
        },       
        pageSize: 10,
        serverPaging: true,
    },
    height: 800,    
    pageable: true,
    columns: [
        "OrderID",
        "Freight",        
        "ShipName",                            
        "ShipCity"        
    ],
    toolbar: [
      { name: "create" },
    ],
    editable : {
      mode: 'popup',
      template: kendo.template($('#popup_editor_template').html())
    },
    edit: function(e) {
        validator = $("#myform").kendoValidator({
         rules: {
           postalCode: function (input) {
             //console.log('in heree1234');
             if (input.is("[name=postcode1]"))
             {
               return validatePostalCode(input);
             }
             if (input.is("[name=postcode2]"))
             {
               return validatePostalCode(input);
             }
             return true;
           }
         }
    }).data("kendoValidator");
    },
    save: function(e) {
        if (! validator.validate() )
      {
        alert('Form has some errors, so form submit is prevented');
        //var errors = validator.errors();
        e.preventDefault();
      }
      else
      {
            alert('Form validated successfully. Now save the form data');
        e.preventDefault();
      }
    }

});
<div id="grid"></div>

<script type="text/html" id="popup_editor_template">
    <div>
    <h4>In Post Code fields, enter value length less than or greater than 4 to see custom validation error message for postcode</h4>
    <div id="myform">
    Address 1 : <input type="text" name="address1" required/> <br/>
    Post code 1 : <input type="number" name="postcode1" required                                                data-postalCode-msg="Postal Code must be four digits"
/> <br/><br/>
    Address 2 : <input type="text" name="address2" required/> </br>
    Post code 2 : <input type="number" name="postcode2" required
                                        data-postalCode-msg="Postal Code must be four digits"
                    /> <br/>
                    </div>
  </div>
</script>

为了实现这一点,我放弃了在
model
字段中使用验证的想法。相反,我使用了
Kendo验证程序
,并将表单验证程序附加到弹出表单

这是我的

下面是我的演示代码:

JS:

<div id="grid"></div>

<script type="text/html" id="popup_editor_template">
    <div>
    <h4>In Post Code fields, enter value length less than or greater than 4 to see custom validation error message for postcode</h4>
    Address 1 : <input type="text" name="address1" required/> <br/>
    Post code 1 : <input type="number" name="postcode1" required                                                data-postalCode-msg="Postal Code must be four digits"
/> <br/><br/>
    Address 2 : <input type="text" name="address2" required/> </br>
    Post code 2 : <input type="number" name="postcode2" required
                                        data-postalCode-msg="Postal Code must be four digits"
                    /> <br/>
  </div>
</script>
function validatePostalCode(input)
{
    //if ( input.is("[data-customPostalCode]") && input.val() )
  if (input.val() )
  {
    console.log("in validatePostalCode: ", input.attr('name'), input.val(), input.val().length);

    //if (input.val().length != 4)
    if( input.val().length != 4 || ( /\D/.test(input.val()) ) )
      return false;
  }
  return true;
}

$("#grid").kendoGrid({
    dataSource: {
        type: "odata",
        transport: {
            read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
        },       
        pageSize: 10,
        serverPaging: true,
        schema: {
            model: {
            fields: {
              postcode1: {
                type: "number",
                defaultValue: null,
                validation: {
                  postalCode: function (input) {
                    console.log('in heree');
                    if (input.is("[name=postcode1]"))
                    {
                      return validatePostalCode(input);
                    }
                    return true;
                  }
                }
              },

              postcode2: {
                type: "number",
                defaultValue: null,
                validation: {
                    //when changing rule name from "postalCode" to "postalCode2", the validation starts working on both fields
                  postalCode: function (input) {
                    console.log('in heree toooo');
                    if (input.is("[name=postcode2]"))
                    {
                      return validatePostalCode(input);
                    }
                    return true;
                  }
                }
              }
            }
        }
    },
    },
    height: 800,    
    pageable: true,
    columns: [
        "OrderID",
        "Freight",        
        "ShipName",                            
        "ShipCity"        
    ],
    toolbar: [
      { name: "create" },
    ],
    editable : {
      mode: 'popup',
      template: kendo.template($('#popup_editor_template').html())
    },
    save: function(e) {
        alert('Now save');
      e.preventDefault();
    }

});
var validator;

function validatePostalCode(input)
{
  if (input.val() )
  {
    //console.log("in validatePostalCode: ", input.attr('name'), input.val(), input.val().length);

    //if (input.val().length != 4)
    if( input.val().length != 4 || ( /\D/.test(input.val()) ) )
      return false;
  }
  return true;
}

$("#grid").kendoGrid({
    dataSource: {
        type: "odata",
        transport: {
            read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
        },       
        pageSize: 10,
        serverPaging: true,
    },
    height: 800,    
    pageable: true,
    columns: [
        "OrderID",
        "Freight",        
        "ShipName",                            
        "ShipCity"        
    ],
    toolbar: [
      { name: "create" },
    ],
    editable : {
      mode: 'popup',
      template: kendo.template($('#popup_editor_template').html())
    },
    edit: function(e) {
        validator = $("#myform").kendoValidator({
         rules: {
           postalCode: function (input) {
             //console.log('in heree1234');
             if (input.is("[name=postcode1]"))
             {
               return validatePostalCode(input);
             }
             if (input.is("[name=postcode2]"))
             {
               return validatePostalCode(input);
             }
             return true;
           }
         }
    }).data("kendoValidator");
    },
    save: function(e) {
        if (! validator.validate() )
      {
        alert('Form has some errors, so form submit is prevented');
        //var errors = validator.errors();
        e.preventDefault();
      }
      else
      {
            alert('Form validated successfully. Now save the form data');
        e.preventDefault();
      }
    }

});
<div id="grid"></div>

<script type="text/html" id="popup_editor_template">
    <div>
    <h4>In Post Code fields, enter value length less than or greater than 4 to see custom validation error message for postcode</h4>
    <div id="myform">
    Address 1 : <input type="text" name="address1" required/> <br/>
    Post code 1 : <input type="number" name="postcode1" required                                                data-postalCode-msg="Postal Code must be four digits"
/> <br/><br/>
    Address 2 : <input type="text" name="address2" required/> </br>
    Post code 2 : <input type="number" name="postcode2" required
                                        data-postalCode-msg="Postal Code must be four digits"
                    /> <br/>
                    </div>
  </div>
</script>
HTML:

<div id="grid"></div>

<script type="text/html" id="popup_editor_template">
    <div>
    <h4>In Post Code fields, enter value length less than or greater than 4 to see custom validation error message for postcode</h4>
    Address 1 : <input type="text" name="address1" required/> <br/>
    Post code 1 : <input type="number" name="postcode1" required                                                data-postalCode-msg="Postal Code must be four digits"
/> <br/><br/>
    Address 2 : <input type="text" name="address2" required/> </br>
    Post code 2 : <input type="number" name="postcode2" required
                                        data-postalCode-msg="Postal Code must be four digits"
                    /> <br/>
  </div>
</script>
function validatePostalCode(input)
{
    //if ( input.is("[data-customPostalCode]") && input.val() )
  if (input.val() )
  {
    console.log("in validatePostalCode: ", input.attr('name'), input.val(), input.val().length);

    //if (input.val().length != 4)
    if( input.val().length != 4 || ( /\D/.test(input.val()) ) )
      return false;
  }
  return true;
}

$("#grid").kendoGrid({
    dataSource: {
        type: "odata",
        transport: {
            read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
        },       
        pageSize: 10,
        serverPaging: true,
        schema: {
            model: {
            fields: {
              postcode1: {
                type: "number",
                defaultValue: null,
                validation: {
                  postalCode: function (input) {
                    console.log('in heree');
                    if (input.is("[name=postcode1]"))
                    {
                      return validatePostalCode(input);
                    }
                    return true;
                  }
                }
              },

              postcode2: {
                type: "number",
                defaultValue: null,
                validation: {
                    //when changing rule name from "postalCode" to "postalCode2", the validation starts working on both fields
                  postalCode: function (input) {
                    console.log('in heree toooo');
                    if (input.is("[name=postcode2]"))
                    {
                      return validatePostalCode(input);
                    }
                    return true;
                  }
                }
              }
            }
        }
    },
    },
    height: 800,    
    pageable: true,
    columns: [
        "OrderID",
        "Freight",        
        "ShipName",                            
        "ShipCity"        
    ],
    toolbar: [
      { name: "create" },
    ],
    editable : {
      mode: 'popup',
      template: kendo.template($('#popup_editor_template').html())
    },
    save: function(e) {
        alert('Now save');
      e.preventDefault();
    }

});
var validator;

function validatePostalCode(input)
{
  if (input.val() )
  {
    //console.log("in validatePostalCode: ", input.attr('name'), input.val(), input.val().length);

    //if (input.val().length != 4)
    if( input.val().length != 4 || ( /\D/.test(input.val()) ) )
      return false;
  }
  return true;
}

$("#grid").kendoGrid({
    dataSource: {
        type: "odata",
        transport: {
            read: "https://demos.telerik.com/kendo-ui/service/Northwind.svc/Orders"
        },       
        pageSize: 10,
        serverPaging: true,
    },
    height: 800,    
    pageable: true,
    columns: [
        "OrderID",
        "Freight",        
        "ShipName",                            
        "ShipCity"        
    ],
    toolbar: [
      { name: "create" },
    ],
    editable : {
      mode: 'popup',
      template: kendo.template($('#popup_editor_template').html())
    },
    edit: function(e) {
        validator = $("#myform").kendoValidator({
         rules: {
           postalCode: function (input) {
             //console.log('in heree1234');
             if (input.is("[name=postcode1]"))
             {
               return validatePostalCode(input);
             }
             if (input.is("[name=postcode2]"))
             {
               return validatePostalCode(input);
             }
             return true;
           }
         }
    }).data("kendoValidator");
    },
    save: function(e) {
        if (! validator.validate() )
      {
        alert('Form has some errors, so form submit is prevented');
        //var errors = validator.errors();
        e.preventDefault();
      }
      else
      {
            alert('Form validated successfully. Now save the form data');
        e.preventDefault();
      }
    }

});
<div id="grid"></div>

<script type="text/html" id="popup_editor_template">
    <div>
    <h4>In Post Code fields, enter value length less than or greater than 4 to see custom validation error message for postcode</h4>
    <div id="myform">
    Address 1 : <input type="text" name="address1" required/> <br/>
    Post code 1 : <input type="number" name="postcode1" required                                                data-postalCode-msg="Postal Code must be four digits"
/> <br/><br/>
    Address 2 : <input type="text" name="address2" required/> </br>
    Post code 2 : <input type="number" name="postcode2" required
                                        data-postalCode-msg="Postal Code must be four digits"
                    /> <br/>
                    </div>
  </div>
</script>

在邮政编码字段中,输入小于或大于4的值长度,以查看邮政编码的自定义验证错误消息
地址1:
邮政编码1:

地址2:
邮政编码2: