简体   繁体   English

如何将 maxlength 设置为 HTML“td”标签(单元格)?

[英]How could I set a maxlength to an HTML "td" tag (cell)?

I have a HTML table with editable cells (every td tag in my table has "contenteditable" set to True).我有一个带有可编辑单元格的 HTML 表格(表格中的每个 td 标签都将“contenteditable”设置为 True)。

The users are meant to fill the table with times in HH:mm format, so I added an onkeypress attribute which triggers a function that checks if the user is entering something other than numbers or the ":" character, and ignores it if that's the case, like so:用户打算用 HH:mm 格式的时间填充表格,所以我添加了一个 onkeypress 属性,它触发一个函数,检查用户是否输入了数字或“:”字符以外的内容,如果是这样,则忽略它情况,像这样:

HTML HTML

<td contenteditable="true" onkeypress="return isNumberKey(event)"> </td>

Javascript Javascript

function isNumberKey(evt){
            let charCode = (evt.which) ? evt.which : event.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=58)
                return false;
            return true;
        }

The problem now is that users can enter more that 5 characters and write stuff like "345:678" for example.现在的问题是用户可以输入超过 5 个字符并编写诸如“345:678”之类的内容。

I would like to know if its possible to limit the number of characters they can enter in the cell to 5 (just like an input tag's "maxlength" attribute), and if it's possible to somehow limit the users to typing two numbers, then a ":" character, then two more numbers.我想知道是否可以将他们可以在单元格中输入的字符数限制为 5(就像输入标签的“maxlength”属性一样),并且是否可以以某种方式限制用户输入两个数字,然后":" 字符,然后是另外两个数字。 (Even though setting a maxlength would already be pretty nice). (即使设置 maxlength 已经很不错了)。

I also wrote a Regex which checks that something is the HH:mm format, but I don't think it's useful in this case since it can only be tested after the user has typed the full five characters, right?我还写了一个 Regex 来检查某些东西是 HH:mm 格式,但我认为它在这种情况下没有用,因为它只能在用户输入完整的五个字符后进行测试,对吧?

Here it is just in case:这是以防万一:

const timeRegex = RegExp("^([0-1]?[0-9]|2[0-3]):[0-5][0-9]$");

Here is the JSFiddle这是JSFiddle

if you already do the checking which characters a user can input into the td with javascript, so why don't just also add the functionality for the current length and other characters to the function too ?如果您已经检查了用户可以使用 javascript 将哪些字符输入到td ,那么为什么不将当前长度和其他字符的功能也添加到函数中呢?

Fe like this: Fe像这样:

function isNumberKey(evt){
            let charCode = (evt.which) ? evt.which : event.keyCode;
            if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=58)
                return false;
                
            var currentContent = evt.target.innerHTML;
            if (currentContent.length >= 5)
                return false;
                
            if (currentContent.length === 2 && charCode !== 58)
                return false;

            return true;
        }

I'm accepting @Ekk4rd 's answer because he really put me on the right track but I still wanted to post an answer with the finished code that matches exactly what I asked in the question.我接受@Ekk4rd 的回答,因为他确实让我走上了正确的轨道,但我仍然想发布一个与我在问题中所问的完全匹配的完成代码的答案。 Here it is:这里是:

HTML HTML

<td contenteditable="true" onkeypress="return isNumberKey(event)"> </td>

Javascript Javascript

        function isNumberKey(evt){
            let charCode = (evt.which) ? evt.which : event.keyCode;
            console.log(evt.target.innerHTML);
            if (evt.target.innerHTML == "<br>" || evt.target.innerHTML == " <br>"){
                evt.target.innerHTML = "";
            }

            let currentContent = evt.target.innerHTML;

            if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode!=58){
                return false;
            }
            
            if (currentContent.length >= 5){
                return false;
            }
                
            if (currentContent.length === 2 && charCode !== 58){
                return false;
            }

            if (currentContent.length != 2 && charCode == 58){
                return false;
            }

            return true;
        }

Here is the JSFiddle这是JSFiddle

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM