Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/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
Javascript ';返回false';不';当对照字符串检查输入时,不能阻止字符出现_Javascript_Html_String - Fatal编程技术网

Javascript ';返回false';不';当对照字符串检查输入时,不能阻止字符出现

Javascript ';返回false';不';当对照字符串检查输入时,不能阻止字符出现,javascript,html,string,Javascript,Html,String,我有下面的代码,我试图检查用户的输入是否在我设置的可接受字符中 代码: element.onkeypress = function (e) { var key = String.fromCharCode(e.which), isAccepted = (typeof self.acceptedChars === "object") ? self.acceptedChars.test(key) : /* Check for RegEx

我有下面的代码,我试图检查用户的输入是否在我设置的可接受字符中

代码:

element.onkeypress = function (e) {
    var
        key = String.fromCharCode(e.which),
        isAccepted = (typeof self.acceptedChars === "object") ?
            self.acceptedChars.test(key) : /* Check for RegEx */
            ~self.acceptedChars.indexOf(key), /* Check for String */
        isMaxOrLess = self.maxLength > this.value.length;
    if (isAccepted && isMaxOrLess) element.dispatchEvent(success);
    else if (!isAccepted && isMaxOrLess) element.dispatchEvent(error);
    return isAccepted && isMaxOrLess;
};
示例:

element.onkeypress = function (e) {
    var
        key = String.fromCharCode(e.which),
        isAccepted = (typeof self.acceptedChars === "object") ?
            self.acceptedChars.test(key) : /* Check for RegEx */
            ~self.acceptedChars.indexOf(key), /* Check for String */
        isMaxOrLess = self.maxLength > this.value.length;
    if (isAccepted && isMaxOrLess) element.dispatchEvent(success);
    else if (!isAccepted && isMaxOrLess) element.dispatchEvent(error);
    return isAccepted && isMaxOrLess;
};
[工作]:如果
self.acceptedChars=/[a-z]/
,则一切都将按预期运行,产生以下结果:

  • 输入:
    ad12s
  • 输出:
    广告
[不工作]:如果
self.acceptedChars=“abc”
,那么即使除了
'a'
'b'
'c'以外的所有字符都将被识别为不接受,出于某种原因,它们仍然可以输入

  • 输入:
    aabb12
  • 输出:
    aabb12
  • 应该是:
    aabb
注意:自定义事件
成功
错误
用于根据
无轴
已接受
条件显示不同的消息,与发生的问题无关

本质上,我的问题是当
acceptedChars
是字符串时,
返回false
不会阻止字符出现。如何解决此问题?

应该是

不是

UPD您需要将值转换为布尔值。不只是让它变假

所以最好少用脚本

self.acceptedChars.indexOf(key) >= 0, /* Check for String */
这应该起作用:

element.onkeypress = function (e) {
    var
        key = String.fromCharCode(e.which),
        isAccepted = (typeof self.acceptedChars === "object") ?
            self.acceptedChars.test(key) : /* Check for RegEx */
            self.acceptedChars.indexOf(key) === -1 ? false : true, /* Check for String */
        isMaxOrLess = self.maxLength > this.value.length;
    if (isAccepted && isMaxOrLess) element.dispatchEvent(success);
    else if (!isAccepted && isMaxOrLess) element.dispatchEvent(error);
    return isAccepted && isMaxOrLess;
};

如果self.acceptedChars是字符串,则应返回真/假值。

波浪线在键之前做什么
~key.indexOf
在这种情况下,它会变成
!=-1到
=0
@TamasRev@TamasRev它将-1转换为falsy(0),留下其他值。@AngelPolitis我已经更新了答案。。您需要将返回值转换为布尔值。不,越神秘越好。我将坚持
~。谢谢@Yury我相信你的解决方案也很有效,但是为什么要用第二个三元来处理这么简单的事情呢?
element.onkeypress = function (e) {
    var
        key = String.fromCharCode(e.which),
        isAccepted = (typeof self.acceptedChars === "object") ?
            self.acceptedChars.test(key) : /* Check for RegEx */
            self.acceptedChars.indexOf(key) === -1 ? false : true, /* Check for String */
        isMaxOrLess = self.maxLength > this.value.length;
    if (isAccepted && isMaxOrLess) element.dispatchEvent(success);
    else if (!isAccepted && isMaxOrLess) element.dispatchEvent(error);
    return isAccepted && isMaxOrLess;
};