Javascript 如何基于值验证内容可编辑段落文本长度
我需要根据contenteditable段落中的文本长度禁用文本键入Javascript 如何基于值验证内容可编辑段落文本长度,javascript,jquery,validation,onkeyup,Javascript,Jquery,Validation,Onkeyup,我需要根据contenteditable段落中的文本长度禁用文本键入 <p id="test" class="given" contenteditable="true"></p> 此getContent方法返回以下示例字符串outout Test [FirstName] ! Lorem Ipsum is [LocationName]. the printing and typesetting. 上面的click-keydown-keyup功能没有按预期工作,我需要做
<p id="test" class="given" contenteditable="true"></p>
此getContent方法返回以下示例字符串outout
Test [FirstName] ! Lorem Ipsum is [LocationName]. the printing and typesetting.
上面的click-keydown-keyup功能没有按预期工作,我需要做的是
我需要考虑[长名]长度为50。
[LocationName]长度为60
因此,为了实现这一点,我需要验证文本输入。如何执行此操作此示例要求第一个方括号[]包含第一个名称,第二个方括号包含LocationName。两者都适当地限制了您上面提到的值 $p.给定。在“单击粘贴键向下键向上”,函数e{ var templateText=$this.text; var fullID=templateText.match/\[^\]]+]/g; iffullID.length==2{ console.log'All:'+templateText.length+'Firstname:'+fullID[0]。length+'LocationName:'+fullID[1]。length; $this.csscolor,黑色; 如果fullID[0]。长度>=50 | | fullID[1]。长度>=60 | | templateText.length>=100&&e.keyCode!==46&&e.keyCode!==8 { 返回false; } }否则{ log“请在括号[]之间键入firstname和LocationName”; $this.csscolor,红色; } };
test[Denzel Washington]!Lorem Ipsum是[好莱坞]。打印和排版。
你能解释一下你想做什么吗…?你是如何将[FirstName]和[LocationName]与中的其他文本内容区分开来的?我们是否错过了另一个正则表达式测试?您是否希望使用类似于短代码格式的括号键入内容?正如第一个括号表示Firstname,第二个括号表示LocationName?@Bilel是的,没有regex或其他实现来隔离[Firstname]和[LocationName]。但我认为它应该有这样的东西。你能给我一个答案吗。第一个括号表示Firstname,第二个括号表示LocationName?不,我只需要假设Firstname的长度为50,LocationName的长度为60。Test [FirstName] ! Lorem Ipsum is [LocationName]. the printing and typesetting.