我必须能够只写字符,有和没有重音符号,每行最多 45 个字符,最多 3 行,从而防止在文本区域中使用 JS 的表情符号。它必须从打字或粘贴开始。这是限制字符的工作部分,但它不会过滤表情符号。记者: $('#config_desc').on('keydown', function(e) { const newLine = /\r*\n/g; const value = e.target.value; const newLines = (value.match(newLine) || []).length; const lines = value.split(newLine); //enter if (e.keyCode === 13 && lines.length >= e.target.rows) { e.preventDefault(); return; } const lineNo = value.substr(0, e.target.selectionStart).split(newLine).length - 1; //backspace if (e.keyCode === 8 && ~value.charAt(e.target.selectionStart - 1).search(newLine)) { if (lines[lineNo].length + lines[lineNo - 1].length <= e.target.cols) return; e.preventDefault(); return; } //del if (e.keyCode === 46 && ~value.charAt(e.target.selectionStart).search(newLine)) { if (lines[lineNo].length + lines[lineNo + 1].length <= e.target.cols) return; e.preventDefault(); return; } if (e.key.length > 1) return; if (value.length < e.target.cols) return; if (lines[lineNo].length > e.target.cols - 1) { if (lines.length < e.target.rows) { const col = (e.target.selectionStart - newLines) / lines.length; let p1 = value.substr(0, e.target.selectionStart); if (col === e.target.cols) { p1 += '\r\n' + String.fromCharCode(e.keyCode); } else { p1 += String.fromCharCode(e.keyCode) + '\r\n'; } e.target.value = p1 + value.substr(e.target.selectionStart, value.length); e.target.selectionStart = p1.length - 1; e.target.selectionEnd = p1.length - 1; } e.preventDefault(); return; } });HTML: <textarea cols="50" rows="3" class="form-control" maxlength="150" id="config_desc" name="config_desc"></textarea>当我添加此正则表达式以禁止表情符号时,无法添加换行等。value.replace(/[^ -\u2122]+ +| *[^ -\u2122]+/ug,'')
添加回答
举报
0/150
提交
取消