0

我有一个文本框,不能输入禁止字符。#.

但是,当文本框被数据填充时,这是可行的,我将焦点放在文本框的中间,然后使用箭头键向左和向右移动,然后跳转到文本框的末尾。

如果我也在文本框的中间键入一个字符,它会再次走到最后

$('[id$=txtClient]').keyup(function () {
        EnableClientValidateButton(); // When the textbox changes, the user has the ability to validate the client
        ChangeColorClient("0"); // The color is changed to white, to notify the user the client is not validated yet.
        var $el = $('[id$=txtClient]'); // the text element to seach for forbidden characters.
        var text = $el.val(); // The value of the textbox
        text = text.split("#").join("");//remove occurances of forbidden characters, in this case #
        $el.val(text);//set it back on the element
    });
4

5 回答 5

3

Javascript 允许您设置输入的光标位置。

我发现了两个有用的功能:

解决方案可能如下所示:

  function getCaretPosition (elem) {

    // Initialize
    var iCaretPos = 0;

    // IE Support
    if (document.selection) {

      // Set focus on the element
      elem.focus ();

      // To get cursor position, get empty selection range
      var oSel = document.selection.createRange ();

      // Move selection start to 0 position
      oSel.moveStart ('character', -elem.value.length);

      // The caret position is selection length
      iCaretPos = oSel.text.length;
    }
    // Firefox support
    else if (elem.selectionStart || elem.selectionStart == '0')
      iCaretPos = elem.selectionStart;

    // Return results
    return (iCaretPos);
  }

  function setCaretPosition(elem, caretPos) {
      if(elem != null) {
          if(elem.createTextRange) {
              var range = elem.createTextRange();
              range.move('character', caretPos);
              range.select();
          }
          else {
              if(elem.selectionStart) {
                  elem.focus();
                  elem.setSelectionRange(caretPos, caretPos);
              }
              else
                  elem.focus();
          }
      }
  }

$('[id$=txtClient]').keyup(function () {
    EnableClientValidateButton(); // When the textbox changes, the user has the ability to validate the client
    ChangeColorClient("0"); // The color is changed to white, to notify the user the client is not validated yet.
    var $el = $('[id$=txtClient]'); // the text element to seach for forbidden characters.
    var text = $el.val(); // The value of the textbox
    text = text.split("#").join("");//remove occurances of forbidden characters, in this case #

    var pos = getCaretPosition(this);
    $el.val(text);//set it back on the element
    setCaretPosition(this, pos);
});
于 2013-06-19T09:55:15.797 回答
1

这有点不愉快,我不是 100% 高兴,但它解决了你遇到的所有给定问题......

$("[id$=txtClient]").keyup(function (e) {
    var text = $(this).val();
    if (text.indexOf("#") > -1) {
        text = text.replace("#", "");
        $(this).val(text);
    }
});

这是一个 jsFiddle 示例...

http://jsfiddle.net/E4cBK/

于 2013-06-19T09:42:47.983 回答
1

您是否尝试过使用该keypress事件?

文档警告平台之间可能存在的行为差异。

至少在 Firefox 中,e.which对应于转换后输入字符的 ascii 码:

$('#txtClient').keypress(function (e) {
    console.log('keypress:', e.which);
    if (e.which == 35) {
        return false;
    }
});

更新的小提琴

于 2013-06-19T10:12:58.837 回答
0

如果用户按下右箭头或左箭头,您可以阻止执行您的代码。为此,您只需添加以下条件:

if(e.which != 37 && e.which != 39){  

您可以在此处找到关键代码。

您的完整代码将是:

$('[id$=txtClient]').keyup(function () {
    if(e.which != 37 && e.which != 39){  
        EnableClientValidateButton();
        ChangeColorClient("0"); 
        var $el = $('[id$=txtClient]'); 
        var text = $el.val(); 
        text = text.split("#").join("");
        $el.val(text);//set it back on the element
    }
});

活生生的例子

于 2013-06-19T09:05:22.533 回答
0

只是防止按键上的默认操作(keydown 不提供一致的字符代码):

$('[id$=txtClient]').keypress(function (e) {
        if (String.fromCharCode(e.which) == '#'){
            e.preventDefault();
        }
});

这只是防止#其余部分保持原样。

干得好;)

于 2013-06-19T09:11:05.660 回答