萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 腳本程式專題 >> JavaScript

jQuery 限制輸入框僅允許數字、字母或英數混合

透過 `keypress` 事件攔截鍵盤輸入、`focus` 禁用輸入法、`paste` 事件檢查剪貼簿內容,三重防護確保輸入框僅接受指定字元集。封裝為 jQuery 外掛,一鍵套用。

三種限制模式

模式允許字元適用場景
onlyNum0-9手機、身分證、數量、價格
onlyAlphaa-z, A-Z英文姓名、代碼、標籤
onlyNumAlpha0-9, a-z, A-Z帳號、密碼、產品編號

完整外掛代碼

;(function($) {
  // 僅數字
  $.fn.onlyNum = function() {
    return this.each(function() {
      var $el = $(this);
      $el.on('keypress', function(e) {
        var code = e.which || e.keyCode;
        // 允許控制鍵:Backspace(8), Tab(9), Enter(13), Delete(46), 方向鍵(37-40)
        if (code <= 46 && (code === 8 || code === 9 || code === 13 || code === 46 || (code >= 37 && code <= 40))) return true;
        if (code >= 48 && code <= 57) return true; // 0-9
        return false;
      })
      .on('focus', function() { this.style.imeMode = 'disabled'; }) // 禁用 IME
      .on('paste', function(e) {
        var text = (e.originalEvent || e).clipboardData.getData('Text');
        return /^\d+$/.test(text);
      });
    });
  };

  // 僅字母
  $.fn.onlyAlpha = function() {
    return this.each(function() {
      var $el = $(this);
      $el.on('keypress', function(e) {
        var code = e.which || e.keyCode;
        if (code <= 46 && (code === 8 || code === 9 || code === 13 || code === 46 || (code >= 37 && code <= 40))) return true;
        if ((code >= 65 && code <= 90) || (code >= 97 && code <= 122)) return true; // A-Z, a-z
        return false;
      })
      .on('focus', function() { this.style.imeMode = 'disabled'; })
      .on('paste', function(e) {
        var text = (e.originalEvent || e).clipboardData.getData('Text');
        return /^[a-zA-Z]+$/.test(text);
      });
    });
  };

  // 英數混合
  $.fn.onlyNumAlpha = function() {
    return this.each(function() {
      var $el = $(this);
      $el.on('keypress', function(e) {
        var code = e.which || e.keyCode;
        if (code <= 46 && (code === 8 || code === 9 || code === 13 || code === 46 || (code >= 37 && code <= 40))) return true;
        if ((code >= 48 && code <= 57) || (code >= 65 && code <= 90) || (code >= 97 && code <= 122)) return true;
        return false;
      })
      .on('focus', function() { this.style.imeMode = 'disabled'; })
      .on('paste', function(e) {
        var text = (e.originalEvent || e).clipboardData.getData('Text');
        return /^[0-9a-zA-Z]+$/.test(text);
      });
    });
  };
})(jQuery);

使用方式

<input type="text" class="only-num" placeholder="僅限數字">
<input type="text" class="only-alpha" placeholder="僅限字母">
<input type="text" class="only-num-alpha" placeholder="英數混合">

<script>
$(function() {
  $('.only-num').onlyNum();
  $('.only-alpha').onlyAlpha();
  $('.only-num-alpha').onlyNumAlpha();
});
</script>

現代化改良(原生 JS + InputEvent)

  • 使用 `input` 事件配合 `e.target.value = value.replace(/[^\d]/g, '')` 過濾,支援中文輸入法組合字、表情符號、拖曳貼上
  • HTML5 `pattern` + `inputmode` 提供原生驗證與鍵盤提示:
<input type="text" pattern="[0-9]*" inputmode="numeric" title="請輸入數字">
<input type="text" pattern="[a-zA-Z]*" inputmode="text" title="請輸入字母">
  • CSS `:invalid` / `:valid` 即時視覺回饋
copyright © 萬盛學電腦網 all rights reserved