【问题标题】:Limit contenteditable span to 5 numerical characters in length将 contenteditable span 的长度限制为 5 个数字字符
【发布时间】:2013-03-24 21:34:36
【问题描述】:

我试图将下面显示的 contenteditable 跨度限制为 5 个字符(4 个数字和一个破折号)。用户需要能够输入从公元前 -9999 年开始的年份。到 2013 年。年份前的破折号表示 B.C.E.年。任何没有破折号的都是公元年。

这些都是可接受的条目:

-9546
-765
-1
0
25
125
2013

<div class="HYPE_element" id="start_year" style="pointer-events: auto; top: 0px; left: 120px; position: absolute; padding: 8px; overflow: visible; word-wrap: break-word; display: inline; z-index: 5; font-size: 16px; color: rgb(120, 204, 187);">
<span contenteditable="true" class="numeric">2013</span>
</div>

现在任何人都可以添加字母数字字符,而且跨度只是不断向右扩展。

我用它来限制字符长度为 5 个。

var content_id = 'start_year';  
    max = 4;
    //binding keyup/down events on the contenteditable div
    $('#'+content_id).keyup(function(e){ check_charcount(content_id, max, e); });
    $('#'+content_id).keydown(function(e){ check_charcount(content_id, max, e); });
    function check_charcount(content_id, max, e)
    {   
        if(e.which != 8 && $('#'+content_id).text().length > max)
        {
           // $('#'+content_id).text($('#'+content_id).text().substring(0, max));
            e.preventDefault();
        }
    }

但我不确定如何将输入的字符限制为数字和破折号。

【问题讨论】:

    标签: jquery html css


    【解决方案1】:

    没有多少浏览器支持这个,但是....

    <input type="number" max="9999"  min="-9999"/>
    

    【讨论】:

    • 我宁愿远离输入文本字段。我已经处理好字符长度,所以最小和最大数字不是问题。我关心的是将输入限制为“0-9”和破折号“-”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    相关资源
    最近更新 更多