不允许的几个字符,例如打字。'<','>'使用jQuery所有输入的文本框 [英] Disallow typing of few of characters e.g.'<', '>' in all input textboxes using jquery

查看:111
本文介绍了不允许的几个字符,例如打字。'<','>'使用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的,这将是容易的。可能是一些将这样开始。

$(输入[类型=文本])

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']")

SOLUTION

我以前都是由@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.

更新

检查我通过如果(!/克,'')THIS.VALUE = this.value.replace(/ [;;&GT&LT]),解决它。还更新的解决方案。

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

再次感谢所有的回答者。

Thanks again to all the answerers.

推荐答案

您应该抓住'KEYUP和模糊事件,并将其附加到bonifies输入功能:不要忘记用户可以粘贴字符序列无效。

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.

对不起,我无法测试我的code,所以你应该把它的暨花岗萨利斯的: - )

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

这篇关于不允许的几个字符,例如打字。'&LT;','&GT;'使用jQuery所有输入的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆