仅IE8 html文本框中的字符

only characters in IE8 html text box

本文关键字:字符 文本 IE8 html      更新时间:2023-09-26

我想限制用户只能在html文本框中输入字符。我正在使用以下代码

 $('.alphaonly').live("input", function () {
     $(this).val($(this).val().replace(/[^A-Za-zÀ-ÿ ]/g, ''));
 });

这在IE9中运行良好,但在IE8中失败。

有人能帮我修吗?

使用插件要容易得多我的意思是:

http://www.thimbleopensource.com/tutorials-snippets/jquery-plugin-filter-text-input

$('#text_input').filter_input({regex:'[a-z]'}); 

对旧浏览器使用keyupmouseup事件:

 $('.alphaonly').on("keyup mouseup input", function () {
     $(this).val($(this).val().replace(/[^A-Za-zÀ-ÿ ]/g, ''));
 });​

CCD_ 3事件在IE<9.使用keyupmouseup可以实现几乎相同的功能。此外,从jQuery 1.7开始,.live()被弃用,取而代之的是.on()

演示

Try this ...
$('.alphaonly').live({
   keyup : function () {
     $(this).val($(this).val().replace(/[^A-Za-zÀ-ÿ ]/g, ''));
   }
});
//If you consider doing it plain JavaScript way, without JQuery      
 function lettersOnly(evt) {
   evt = (evt) ? evt : event;
   var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
      ((evt.which) ? evt.which : 0));
   if (charCode > 31 && (charCode < 65 || charCode > 90) &&
      (charCode < 97 || charCode > 122)) {
      alert("Enter letters only.");
      return false;
   }
   return true;
 }