gpt4 book ai didi

javascript - slickgrid 使用正则表达式验证列

转载 作者:行者123 更新时间:2023-12-04 01:21:20 25 4
gpt4 key购买 nike

有一个带有列验证的简单示例:

    function requiredFieldValidator(value) {
if (value == null || value == undefined || !value.length) {
return {valid: false, msg: "This is a required field"};
} else {
return {valid: true, msg: null};
}
}

要验证列,只需输入此选项:validator: requiredFieldValidator

但是如果我需要传递额外的参数 - 正则表达式字符串,我该如何使用正则表达式函数。

最佳答案

无论如何,在我看来,解决这个问题的最佳方法是编写您自己的编辑器,您将把它作为另一个新的自定义编辑器添加到 slick.editor.js 中。该文件也是为此而制作的。我确实实现了正则表达式测试,效果很好。

这是我的新编辑器,它不仅适用于正则表达式,还适用于各种条件类型,例如 min:2 的选项需要最小数量为 2,而 minLength :2 将要求输入是至少 2 个字符的字符串,等等...现在对于您真正要寻找的那个,那将是我的代码定义中的 pattern 类型。所以基本上,你必须在 slick.editor.js 中包含这段代码:

ConditionalCellEditor : function(args) {            
var $input;
var defaultValue;
var scope = this;
var condObj = null;

this.init = function() {
$input = $("<INPUT type='text' class='editor-text' />")
.appendTo(args.container)
.bind("keydown.nav", function(e) {
if (e.keyCode === $.ui.keyCode.LEFT || e.keyCode === $.ui.keyCode.RIGHT) {
e.stopImmediatePropagation();
}
})
.focus()
.select();
};

this.destroy = function() {
$input.remove();
};

this.focus = function() {
$input.focus();
};

this.getValue = function() {
return $input.val();
};

this.setValue = function(val) {
$input.val(val);
};

this.loadValue = function(item) {
defaultValue = item[args.column.field] || "";
$input.val(defaultValue);
$input[0].defaultValue = defaultValue;
$input.select();
};

this.serializeValue = function() {
return $input.val();
};

this.applyValue = function(item,state) {
item[args.column.field] = state;
};

this.isValueChanged = function() {
return (!($input.val() == "" && defaultValue == null)) && ($input.val() != defaultValue);
};

this.validate = function() {
var condObj = args.column.editorOptions;
var returnMsg = null;
var returnValid = true;

if(typeof condObj.min != 'undefined') {

if( parseFloat($input.val()) < parseFloat(condObj.min) ) {
returnMsg = "Value should not be less then "+condObj.min;
returnValid = false;
}
}
if(typeof condObj.max != 'undefined') {
if( parseFloat($input.val()) > parseFloat(condObj.max) ) {
returnMsg = "Value should not be over "+condObj.max;
returnValid = false;
}
}
if(typeof condObj.minLength != 'undefined') {

if($input.val().length < condObj.minLength) {
returnMsg = "Value length should not be less then "+condObj.minLength;
returnValid = false;
}
}
if(typeof condObj.maxLength != 'undefined') {
if($input.val().length > condObj.maxLength) {
returnMsg = "Value length should not be over "+condObj.maxLength;
returnValid = false;
}
}
if(typeof condObj.pattern != 'undefined') {
if( condObj.pattern.test($input.val()) != true ) {
returnMsg = (condObj.msg) ? condObj.msg : "Value is invalid following a regular expression pattern";
returnValid = false;
}
}
if(typeof condObj.required != 'undefined') {
if($input.val().length == "" && condObj.required) {
returnMsg = "Required field, please provide a value";
returnValid = false;
}
}

// Now let's return our boolean results and message if invalid
return {
valid: returnValid,
msg: returnMsg
}
};

this.init();
},

然后在我的 SlickGrid 列定义中,我调用我定义的新编辑器并传递一些我决定作为对象传递到 editorOptions 的选项,这让我可以更灵活地添加任何我想要的选项、模式、msg、minLength 等......全部合二为一。我的示例是用于电子邮件正则表达式模式验证。

columns1 = [
...
{id:"email", field:"email", name:"Em@il", width:145, editor:ConditionalCellEditor, editorOptions:{pattern:/^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/, msg:"Must be a valid email"} },
...];

瞧,就像一个魅力!我几乎不再使用 editor:TextCellEditor 了,因为我的新 ConditionalCellEditor 编辑器给了我更多的灵 active 。希望它有所帮助,让我知道它是怎么回事......

关于javascript - slickgrid 使用正则表达式验证列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9180828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com