slickgrid validate column with regex

前端 未结 4 627
心在旅途
心在旅途 2021-01-03 08:39

There is a simple sample with column validation:

    function requiredFieldValidator(value) {
      if (value == null || value == undefined || !value.length)         


        
4条回答
  •  伪装坚强ぢ
    2021-01-03 09:14

    In order to extend the answer of @mike-gwilt, you can use the cellAttrs column options to specify (in your column definition) the regular expressions and the message to report, like this:

    ... { id: "columnId", name: "columnName", validator: RegexValidator, cellAttrs: {"reg":"^\\d{1,2}$", "msg": "The value should be a number of two digits."} ...
    

    Then the html generated looks something like this:

    Finally, as you can access the input element inside your validation function, define it like this:

     function RegexValidator(value, input) {
        var msg = input.parent().attr('msg');
        var reg = new RegExp(input.parent().attr('reg'));
        if (!reg.exec(value)) {
            return { valid: false, msg: msg};
        } else {
            return { valid: true, msg: null };
        }
     }
    

提交回复
热议问题