gpt4 book ai didi

javascript - ExtJS 自定义验证器 colorpicker

转载 作者:行者123 更新时间:2023-11-27 23:12:30 26 4
gpt4 key购买 nike

我在 ExtJS 中有一个颜色选择器,我用它来选择颜色作为背景。不过我希望用户能够输入自己的十六进制代码。为了确保他们不会将无效的十六进制代码放入字段中,我想验证它们是否具有正确数量的字符。

用户还可以输入名称颜色,例如:黑色、红色、金属亮蓝色。

The SenchaDocumentation on validators was.. Less than helpful.

validator: function (val) {
// remove non-numeric characters
var tn = val.replace(/[^0-9]/g,''),
errMsg = "Must be a 10 digit telephone number";
// if the numeric value is not 10 digits return an error message
return (tn.length === 10) ? true : errMsg;
}

一旦我尝试使用自定义验证器,我的颜色选择器就会消失。有人可以向我展示更全面的验证器使用指南吗?谢谢。

编辑

来自 ExtJS 的选择器源代码上的我的扩展 .js 文件

Ext.define('name.name.colorpick.Selector', {
extend: 'Ext.ux.colorpick.Selector',
xtype: 'xtype-xtype-colorpick-selector',

editable:false,

getSliderAndAField: function() {
return [];
},

getMapAndHexRGBFields: function(){
var me = this;
var result = this.callParent(arguments);
var hexField = result.items[1].items[0];
hexField.disabled = true;
hexField.listeners = {
change: function(field,value){
me.setValue(value);
}
};

return result;
},

getSliderAndHField: function (){
var me = this;
var result = this.callParent(arguments);
var hField = result.items[1];
hField.disabled = true;

return result;
},

getSliderAndSField: function (){
var me = this;
var result = this.callParent(arguments);
var sField = result.items[1];
sField.disabled = true;

return result;

},

getSliderAndVField: function (){
var me = this;
var result = this.callParent(arguments);
var vField = result.items[1];
vField.disabled = true;

return result;

}

});

最佳答案

基于Validating css color names以下是如何为十六进制字段定义验证器。我假设您希望 hexField 可编辑。

getMapAndHexRGBFields: function (childViewModel) {
var me = this;
var result = this.callParent(arguments);
var hexField = result.items[1].items[0];
hexField.readOnly = false;
hexField.validator= function (val) {
var validateColor = function(stringToTest) {
if (stringToTest === "") {
return false;
}
if (stringToTest === "inherit") { return false; }
if (stringToTest === "transparent") { return false; }

var image = document.createElement("img");
image.style.color = "rgb(0, 0, 0)";
image.style.color = stringToTest;
if (image.style.color !== "rgb(0, 0, 0)") { return true; }
image.style.color = "rgb(255, 255, 255)";
image.style.color = stringToTest;
return image.style.color !== "rgb(255, 255, 255)";
};

var isValid = validateColor(val);

var errMsg = "Not a valid Color";
return isValid ? true : errMsg;
};
hexField.listeners = {
change: function(field, value){
if(field.isValid()) {
me.setValue(value);
}
}
};
return result;
}

关于javascript - ExtJS 自定义验证器 colorpicker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36086620/

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