【问题标题】:Block a certain character in textArea阻止 textArea 中的某个字符
【发布时间】:2021-04-20 15:22:26
【问题描述】:

基本上我有一个文本区域,用户无法输入“%”,不需要显示警告/警报,最简单的方法是什么?

关于如何处理这个问题有什么建议吗? 我不知道我是否必须创建一个函数来完成这项工作......

<div class="col-md-12 label-base">

                <label for="exampleFormControlTextarea1">Justify</label>

                <textarea style="resize: none"  ng-disabled="negociacaoEspecCtrl.proposta.flagDesabilitaEdicaoProposta"
                 class="form-control observacoes" id="exampleFormControlTextarea1" rows="3" ng-model="negociacaoEspecCtrl.proposta.dadosCadastro.negociacaoEspecial.justificativaNegociacaoEspecial"> 
               </textarea>
</div>

【问题讨论】:

标签: javascript html textarea


【解决方案1】:

您可以使用.replaceAll()。向 textarea 添加一个事件监听器,并在每次用户输入 textarea 时将每次出现的% 替换为一个空白字符串。

var txtarea = document.getElementById("exampleFormControlTextarea1");
txtarea.addEventListener("input", function() {
  txtarea.value = txtarea.value.replaceAll("%", "");
})
<div class="col-md-12 label-base">

  <label for="exampleFormControlTextarea1">Justify</label>

  <textarea style="resize: none" ng-disabled="negociacaoEspecCtrl.proposta.flagDesabilitaEdicaoProposta" class="form-control observacoes" id="exampleFormControlTextarea1" rows="3" ng-model="negociacaoEspecCtrl.proposta.dadosCadastro.negociacaoEspecial.justificativaNegociacaoEspecial">
</textarea><br/>
Try entering '%' into the textarea above
</div>

这可以防止用户粘贴/输入字符%

为了更好地支持.replaceAll(),请查看以下问题:Javascript .replaceAll() is not a function type error

这是一个在用户输入%时通知用户的示例:

var txtarea = document.getElementById("exampleFormControlTextarea1");
txtarea.addEventListener("input", function() {
  var b4 = txtarea.value;
  txtarea.value = txtarea.value.replaceAll("%", "");
  if(txtarea.value != b4){
    console.log("You tried to enter %");
  }
})
<div class="col-md-12 label-base">

  <label for="exampleFormControlTextarea1">Justify</label>

  <textarea style="resize: none" ng-disabled="negociacaoEspecCtrl.proposta.flagDesabilitaEdicaoProposta" class="form-control observacoes" id="exampleFormControlTextarea1" rows="3" ng-model="negociacaoEspecCtrl.proposta.dadosCadastro.negociacaoEspecial.justificativaNegociacaoEspecial">
</textarea><br/>
Try entering '%' into the textarea above
</div>

或者你可以拦截 keydown 事件,虽然这不会阻止粘贴:

var txtarea = document.getElementById("exampleFormControlTextarea1");
txtarea.addEventListener("keydown", function(e) {
  e = e || window.event;
  if(e.key == "%"){
    e.preventDefault();
  }
})
<div class="col-md-12 label-base">

  <label for="exampleFormControlTextarea1">Justify</label>

  <textarea style="resize: none" ng-disabled="negociacaoEspecCtrl.proposta.flagDesabilitaEdicaoProposta" class="form-control observacoes" id="exampleFormControlTextarea1" rows="3" ng-model="negociacaoEspecCtrl.proposta.dadosCadastro.negociacaoEspecial.justificativaNegociacaoEspecial">
</textarea><br/>
Try entering '%' into the textarea above
</div>

【讨论】:

  • js代码,我会把它放到一个函数中吗?我的 html 没有
    和 标签,只有 div;所以我没有设法使用
  • @PedroPastori 没有
【解决方案2】:

您可以为此使用 Event.preventDefault()。

如果事件是可取消的,该方法将取消该事件,这意味着属于该事件的默认动作不会发生,因此在我们的例子中,它将取消添加到文本区域的字符,如果它是'% '(ASCII 码中的 37)

var textarea = document.getElementById("textArea");
textarea.onkeypress = function(e) {
    if (e.which === 37){
    e.preventDefault();
  }
};
<textarea id="textArea" rows="3" placeholder="enter text without %">
</textarea>

【讨论】:

  • 您仍然可以将字符粘贴到文本区域中。
  • 我非常抱歉@Ran。我正在编辑我的代码,不知何故我编辑了你的。真的很抱歉
  • 感谢@spectric 将其还原
  • 我需要将脚本放入函数中吗?我不知道为什么,但我试图把它放到
  • 您是否也将 html 添加到正文中?
【解决方案3】:

textarea 中的输入添加事件列表,当用户输入% 时,从textarea 值中删除最后一个字符。

const textArea = document.querySelector("textarea");
textArea.addEventListener("input", (e) => {
    textArea.value = textArea.value.replaceAll("%", "");
});
<div class="col-md-12 label-base">

  <label for="exampleFormControlTextarea1">Justify</label>

  <textarea></textarea>
</div>

【讨论】:

  • @Spectric 在这种情况下,我们需要使用regex。和你的答案一模一样
猜你喜欢
  • 2014-11-13
  • 1970-01-01
  • 2019-06-16
  • 2020-06-15
  • 1970-01-01
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多