Javascript 使用knockout extender不允许使用字符数组
我一直在读关于淘汰赛扩展器的文章 我试图找出一个输入,我想一个扩展器,有一个特殊字符数组,不允许特殊字符进入输入。但我恐怕搞不清楚我在做什么Javascript 使用knockout extender不允许使用字符数组,javascript,knockout.js,Javascript,Knockout.js,我一直在读关于淘汰赛扩展器的文章 我试图找出一个输入,我想一个扩展器,有一个特殊字符数组,不允许特殊字符进入输入。但我恐怕搞不清楚我在做什么 this.firstName = ko.observable("").extend({doNotAllow: ['<','>','%','&']}); ko.extenders.doNotAllow = function(target, doNotAllow) { /*replace any occurrences spe
this.firstName = ko.observable("").extend({doNotAllow: ['<','>','%','&']});
ko.extenders.doNotAllow = function(target, doNotAllow) {
/*replace any occurrences specialchars with nothing */
return target;
};
this.firstName=ko.observable(“”).extend({doNotAllow:['''''%,'&']});
ko.extenders.doNotAllow=函数(目标,doNotAllow){
/*将所有出现的特殊字符替换为零*/
回报目标;
};
如果要使用extend
删除这些字符,只需在extender函数中使用正则表达式
来验证字符串,然后用新值更新
原始可观测值即可。
工作示例: 使用ko.extend
function AppViewModel(first, last) {
this.firstName = ko.observable(first).extend({ doNotAllow:['<','>','%','&'] });
}
ko.extenders.doNotAllow = function(target, charachters) {
target.validationMessage = ko.observable();
//define a function to do validation for special characters
function validate(newValue) {
// you can change regularExpression based on what you exactly want to be removed by using charachters parameter or just changing below expression
target(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
//initial validation
validate(target());
//validate whenever the value changes
target.subscribe(validate);
//return the original observable
return target;
};
ko.applyBindings(new AppViewModel("Type Special Characters"));
function AppViewModel(first) {
var self = this;
self.firstName = ko.observable(first);
self.firstName.subscribe(function (newValue) {
if (newValue) {
self.firstName(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
});
}
ko.applyBindings(new AppViewModel("Type Special Characters"));
函数AppViewModel(第一个,最后一个){
this.firstName=ko.observable(first).extend({doNotAllow:[''''','&']});
}
ko.extenders.doNotAllow=函数(目标,字符){
target.validationMessage=ko.observable();
//定义一个函数来验证特殊字符
函数验证(newValue){
//通过使用charachters参数或仅更改下面的表达式,可以根据您确切想要删除的内容更改regularExpression
目标(newValue.replace(/[&\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
}
//初步验证
验证(target());
//每当值更改时进行验证
目标。订阅(验证);
//返回原始的可观测值
回报目标;
};
应用绑定(新的AppViewModel(“键入特殊字符”);
HTML:
<input data-bind='value: firstName, valueUpdate: "afterkeydown"' />
下面是一个简单的方法,你想做什么 使用非ko.extend
function AppViewModel(first, last) {
this.firstName = ko.observable(first).extend({ doNotAllow:['<','>','%','&'] });
}
ko.extenders.doNotAllow = function(target, charachters) {
target.validationMessage = ko.observable();
//define a function to do validation for special characters
function validate(newValue) {
// you can change regularExpression based on what you exactly want to be removed by using charachters parameter or just changing below expression
target(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
//initial validation
validate(target());
//validate whenever the value changes
target.subscribe(validate);
//return the original observable
return target;
};
ko.applyBindings(new AppViewModel("Type Special Characters"));
function AppViewModel(first) {
var self = this;
self.firstName = ko.observable(first);
self.firstName.subscribe(function (newValue) {
if (newValue) {
self.firstName(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
});
}
ko.applyBindings(new AppViewModel("Type Special Characters"));
函数AppViewModel(第一个){
var self=这个;
self.firstName=ko.可观察(第一);
self.firstName.subscribe(函数(newValue){
如果(新值){
self.firstName(newValue.replace(/[&\/\\\\\\\\\\,+()$~%.':*?{}]/g',);
}
});
}
应用绑定(新的AppViewModel(“键入特殊字符”);
HTML:
<input data-bind='textInput: firstName' />
如果要使用
extend
删除这些字符,只需在extender函数中使用正则表达式
来验证字符串,然后用新值更新
原始可观测值即可。
工作示例: 使用ko.extend
function AppViewModel(first, last) {
this.firstName = ko.observable(first).extend({ doNotAllow:['<','>','%','&'] });
}
ko.extenders.doNotAllow = function(target, charachters) {
target.validationMessage = ko.observable();
//define a function to do validation for special characters
function validate(newValue) {
// you can change regularExpression based on what you exactly want to be removed by using charachters parameter or just changing below expression
target(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
//initial validation
validate(target());
//validate whenever the value changes
target.subscribe(validate);
//return the original observable
return target;
};
ko.applyBindings(new AppViewModel("Type Special Characters"));
function AppViewModel(first) {
var self = this;
self.firstName = ko.observable(first);
self.firstName.subscribe(function (newValue) {
if (newValue) {
self.firstName(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
});
}
ko.applyBindings(new AppViewModel("Type Special Characters"));
函数AppViewModel(第一个,最后一个){
this.firstName=ko.observable(first).extend({doNotAllow:[''''','&']});
}
ko.extenders.doNotAllow=函数(目标,字符){
target.validationMessage=ko.observable();
//定义一个函数来验证特殊字符
函数验证(newValue){
//通过使用charachters参数或仅更改下面的表达式,可以根据您确切想要删除的内容更改regularExpression
目标(newValue.replace(/[&\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
}
//初步验证
验证(target());
//每当值更改时进行验证
目标。订阅(验证);
//返回原始的可观测值
回报目标;
};
应用绑定(新的AppViewModel(“键入特殊字符”);
HTML:
<input data-bind='value: firstName, valueUpdate: "afterkeydown"' />
下面是一个简单的方法,你想做什么 使用非ko.extend
function AppViewModel(first, last) {
this.firstName = ko.observable(first).extend({ doNotAllow:['<','>','%','&'] });
}
ko.extenders.doNotAllow = function(target, charachters) {
target.validationMessage = ko.observable();
//define a function to do validation for special characters
function validate(newValue) {
// you can change regularExpression based on what you exactly want to be removed by using charachters parameter or just changing below expression
target(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
//initial validation
validate(target());
//validate whenever the value changes
target.subscribe(validate);
//return the original observable
return target;
};
ko.applyBindings(new AppViewModel("Type Special Characters"));
function AppViewModel(first) {
var self = this;
self.firstName = ko.observable(first);
self.firstName.subscribe(function (newValue) {
if (newValue) {
self.firstName(newValue.replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '') );
}
});
}
ko.applyBindings(new AppViewModel("Type Special Characters"));
函数AppViewModel(第一个){
var self=这个;
self.firstName=ko.可观察(第一);
self.firstName.subscribe(函数(newValue){
如果(新值){
self.firstName(newValue.replace(/[&\/\\\\\\\\\\,+()$~%.':*?{}]/g',);
}
});
}
应用绑定(新的AppViewModel(“键入特殊字符”);
HTML:
<input data-bind='textInput: firstName' />
Hmm?目前,您的代码根本不起任何作用。您是否尝试过任何类型的实现?让我们知道具体出了什么问题,这样我们可以更容易地提供帮助。嗯?目前,您的代码根本不起任何作用。您是否尝试过任何类型的实现?让我们知道具体出了什么问题,以便我们能够更轻松地提供帮助。