禁止输入少量字符,例如'<'、'&a

Disallow typing of few of characters e.g.#39;lt;#39;, #39;gt;#39; in all input textboxes using jquery(禁止输入少量字符,例如lt;、gt;在使用 jquery 的所有输入文本框中)
本文介绍了禁止输入少量字符,例如'<'、'>'在使用 jquery 的所有输入文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何做到这一点:-
当用户输入像 'abcd' 这样的字符然后 '>'(我的应用程序的无效字符)时,我想将文本设置回 'abcd'.如果我们可以像在 winforms 应用程序中那样取消输入本身,那就更好了.这应该发生在用户输入而不是点击按钮时.

How do I achieve this:-
When user types character like 'abcd' and then '>'(an invalid character for my application), I want to set the text back to 'abcd'. Better if we can cancel the input itself as we do in winforms application. This should happen when user is typing and not on a click of button.

我希望将其应用于我网页中的所有文本框.如果解决方案基于 jQuery,这将很容易.可能是这样开始的.
$("input[type='text']")

I want this to be applied on all text boxes in my web page. This will be easy if the solution is jQuery based. May be something which will start like this.
$("input[type='text']")

解决方案
我使用了@jAndy 和@Iacopo 提供的两个答案(对不起,不能标记为两个答案)如下.

SOLUTION
I used both of the answer provided by @jAndy and @Iacopo (Sorry, couldn't mark as answer to both) as below.

$(document).ready(function() {
  //makes sure that user cannot enter < or > sign in text boxes.
  $("input:text").keyup(purgeInvalidChars)
      .blur(purgeInvalidChars)
      .bind('keypress', function(event) {
        if (event.which === 62 || event.which === 60)
          return (false);
      });
  function purgeInvalidChars() {
    if (this.value != this.value.replace(/[<>]/g, '')) {
      this.value = this.value.replace(/[<>]/g, '');
    }
  }
});

虽然仍然存在一个问题,即它不允许用户在文本框中选择文本,并且这只发生在 chrome 上,但在 IE 上工作正常(第一次:D),未在 Firefox 上测试.如果有人能找到解决方案或希望谷歌的人解决它会很高兴:D.

Though one issue still remained i.e. It doesn't allow user to select text in the textbox and this only happens on chrome, work fine on IE (for the first time :D), not tested on firefox. It would be glad if anyone can find solution for that or hope people at Google solves it :D.

更新
我通过 if (this.value != this.value.replace(/[<>]/g, '')) 检查解决了它.还更新了解决方案.

UPDATE
I solved it by if (this.value != this.value.replace(/[<>]/g, '')) check. Also updated solution.

再次感谢所有回答者.

推荐答案

您应该捕获 'keyup' 和 'blur' 事件,并将它们附加到一个函数,该函数可以对输入进行组合:不要忘记用户可以粘贴无效的字符序列.

You should catch the 'keyup' and 'blur' events and attach them to a function that bonifies the input: don't forget that the user can paste an invalid sequence of characters.

例如

$('input[type="text"]').keyup(purgeInvalidChars).blur(purgeInvalidChars)

function purgeInvalidChars()
{
   this.value = this.value.replace(/[<>]/g, ''); 
} 

您肯定会改进正则表达式,也许会替换除已启用字符之外的所有字符.

surely you will improve the regexp, maybe replacing all characters except the enabled ones.

对不起,我无法测试我的代码,所以你应该把它cum grano salis :-)

Sorry, I can't test my code, so you should take it cum grano salis :-)

这篇关于禁止输入少量字符,例如'&lt;'、'&gt;'在使用 jquery 的所有输入文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)
Bind multiple parameters from route and body to a model in ASP.NET Core(在ASP.NET Core中将路由和主体中的多个参数绑定到一个模型)
Custom model binding in AspNet Core WebApi?(AspNet Core WebApi中的自定义模型绑定?)
How to minify in .net core mvc view?(如何在.Net核心MVC视图中缩小?)