Javascript 逗号分隔数字列表的掩码

Javascript 逗号分隔数字列表的掩码,javascript,jquery,Javascript,Jquery,我试图为我的输入创建一个逗号分隔值的掩码,但我遇到了一些困难 你们能帮帮我吗 这是我目前的面具: <script> $(document).on("keyup keydown keypress", "commaseparated", function (e) { if (!$(this).val().match(/^([0-9]{0,1})(,[0-9]{1,}){1,}?$/g)) { e.preventDefault();

我试图为我的输入创建一个逗号分隔值的掩码,但我遇到了一些困难

你们能帮帮我吗

这是我目前的面具:

<script>
    $(document).on("keyup keydown keypress", "commaseparated", function (e) {
        if (!$(this).val().match(/^([0-9]{0,1})(,[0-9]{1,}){1,}?$/g)) {
            e.preventDefault();
        }
    });
</script>
<input type="text" class="commaseparated" />
<!-- example input would be
     1235 <- single number
     or
     2654,524874,5456 <- many numbers
-->

请尝试一下下面的jquery

//We need to capture the keypress event, as this is triggered before the value is displayed in the text box.
$(document).on("keypress", ".commaseparated", function (e) {
    var currentValue = $(this).val();
    //If the value is empty, let user enter numerals.
    if(currentValue == "" && e.keyCode >= 48 && e.keyCode <= 57) {
        return true;
    }
    else {
        //If some value is there, get the last character.
        var lastChar = currentValue.substr(currentValue.length - 1);

        //If the last character is comma, let user enter numerals.
        if(lastChar == "," && e.keyCode >= 48 && e.keyCode <= 57){
            return true;
        }
        //If the last character is comma, deny entering comma again. 
        else if(lastChar == "," && e.keyCode == 44){
            return false;
        }
        //If the control reaches here and as last character could only be numeral, let user enter only comma or numerals. 
        else if (e.keyCode == 44 || (e.keyCode >= 48 && e.keyCode <= 57)) {
            return true;
        }
        // for everything else, return false. 
        else{
            return false;
        }
    }
});

找到JSFIDLE。

请尝试下面的jquery

//We need to capture the keypress event, as this is triggered before the value is displayed in the text box.
$(document).on("keypress", ".commaseparated", function (e) {
    var currentValue = $(this).val();
    //If the value is empty, let user enter numerals.
    if(currentValue == "" && e.keyCode >= 48 && e.keyCode <= 57) {
        return true;
    }
    else {
        //If some value is there, get the last character.
        var lastChar = currentValue.substr(currentValue.length - 1);

        //If the last character is comma, let user enter numerals.
        if(lastChar == "," && e.keyCode >= 48 && e.keyCode <= 57){
            return true;
        }
        //If the last character is comma, deny entering comma again. 
        else if(lastChar == "," && e.keyCode == 44){
            return false;
        }
        //If the control reaches here and as last character could only be numeral, let user enter only comma or numerals. 
        else if (e.keyCode == 44 || (e.keyCode >= 48 && e.keyCode <= 57)) {
            return true;
        }
        // for everything else, return false. 
        else{
            return false;
        }
    }
});
查找JSFIDLE。

您可以使用RegExp/,|\D?=\1 | ^[,\D]| ^[^,\D]$/g匹配逗号或非数字字符,后跟逗号或非数字字符,或字符串开头的逗号或非数字字符,或字符串的最后一个字符不是数字或逗号,请用空字符串替换匹配

$.commaseparated.oninput,function e{ $e.target.propvalue,函数,val{ 返回val.replace/,|\D?=\1 | ^[,\D]|[^,\D]$/g; } }; 您可以使用RegExp/,|\D?=\1 | ^[,\D]|[^,\D]$/g匹配逗号或非数字字符,后跟逗号或非数字字符,或字符串开头的逗号或非数字字符,或字符串的最后一个字符不是数字或逗号,请用空字符串替换匹配

$.commaseparated.oninput,function e{ $e.target.propvalue,函数,val{ 返回val.replace/,|\D?=\1 | ^[,\D]|[^,\D]$/g; } };
预期结果是什么?@guest271314我编辑了问题预期结果是什么?@guest271314我编辑了问题有趣。您能在代码中添加一些注释以便更好地解释它吗?太好了D谢谢你!有趣的您能在代码中添加一些注释以便更好地解释它吗?太好了D谢谢你!这不起作用,因为|在字符集中并不表示“或”,因此正则表达式允许输入字符{、}和|。这可以在代码段中进行验证。不幸的是,我的正则表达式不够聪明,无法找出正确的正则表达式。哎呀,我在上面的注释中错误地允许使用的字符列表中漏掉了+字符……在SO停止编辑注释2分钟后发现我的遗漏:/regex应该以$结尾吗?用$i可以在字符串中间键入非数字,但不能在结尾键入。如果我删除了$,那么我就不能在任何地方输入非数字。不管怎样,谢谢你在写了两年后更新了你的答案。我学到了更多关于正则表达式,谢谢你的编辑。@保罗,我可以键入一个非数字在中间的字符串不能输入字符不是一个数字或不是逗号这里。嗯,也许它是我的浏览器FF 52。我输入了1,2,3,然后使用箭头键在2和之间移动光标,。我键入了a,然后将光标移到文本的末尾,继续键入数字和逗号。尽管如此,这是一个边缘案例,我们进行后端验证…您的答案非常好,仍然非常有用!这不起作用,因为|在字符集中并不表示“或”,因此正则表达式允许输入字符{、}和|。这可以在代码段中进行验证。不幸的是,我的正则表达式不够聪明,无法找出正确的正则表达式。哎呀,我在上面的注释中错误地允许使用的字符列表中漏掉了+字符……在SO停止编辑注释2分钟后发现我的遗漏:/regex应该以$结尾吗?用$i可以在字符串中间键入非数字,但不能在结尾键入。如果我删除了$,那么我就不能在任何地方输入非数字。不管怎样,谢谢你在写了两年后更新了你的答案。我学到了更多关于正则表达式,谢谢你的编辑。@保罗,我可以键入一个非数字在中间的字符串不能输入字符不是一个数字或不是逗号这里。嗯,也许它是我的浏览器FF 52。我输入了1,2,3,然后使用箭头键在2和之间移动光标,。我键入了a,然后将光标移到文本的末尾,继续键入数字和逗号。尽管如此,这是一个边缘案例,我们进行后端验证…您的答案非常好,仍然非常有用!