【问题标题】:How to filter a TextBox accept only english alphabetic characters In jquery?如何在 jquery 中过滤 TextBox 只接受英文字母字符?
【发布时间】:2016-06-20 07:13:09
【问题描述】:

我有一个文本框,我想用英文字母字符过滤它在 jquery.any 帮助?

【问题讨论】:

标签: jquery


【解决方案1】:

   $('.acceptalpha').bind('keyup blur',function()
{ 
       var thistext = $(this);
       thistext.val(thistext.val().replace(/[^a-z]/g,'') ); 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input name="text" id="acceptalpha" class="acceptalpha">

【讨论】:

  • @Mohsend 如果对您有用,您可以将其标记为已完成
【解决方案2】:

试试:

$("#mytextbox").on("keypress", function(event) {

    // Disallow anything not matching the regex pattern (A to Z uppercase, a to z lowercase and white space)
    // For more on JavaScript Regular Expressions, look here: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Regular_Expressions
    var englishAlphabetAndWhiteSpace = /[A-Za-z ]/g;

    // Retrieving the key from the char code passed in event.which
    // For more info on even.which, look here: http://stackoverflow.com/q/3050984/114029
    var key = String.fromCharCode(event.which);

    //alert(event.keyCode);

    // For the keyCodes, look here: http://stackoverflow.com/a/3781360/114029
    // keyCode == 8  is backspace
    // keyCode == 37 is left arrow
    // keyCode == 39 is right arrow
    // englishAlphabetAndWhiteSpace.test(key) does the matching, that is, test the key just typed against the regex pattern
    if (event.keyCode == 8 || event.keyCode == 37 || event.keyCode == 39 || englishAlphabetAndWhiteSpace.test(key)) {
        return true;
    }

    // If we got this far, just return false because a disallowed key was typed.
    return false;
});

$('#mytextbox').on("paste",function(e)
{
    e.preventDefault();
});

【讨论】:

    猜你喜欢
    • 2012-01-09
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 2010-10-21
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 2014-06-06
    相关资源
    最近更新 更多