You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

输入控件数字序列长度限制开发需求:禁止输入超7位数字序列

Solution to Restrict Long Numeric Sequences (Including Those With Spaces)

Let's fix your script to properly handle the requirement: blocking or obfuscating any numeric sequence (even with spaces between digits) that exceeds 7 digits total. Your original code only checks individual space-separated chunks, which misses cases like 99 9 9999 9999 (total digits = 11, but each chunk is under 7).

Key Improvements Needed

  • Catch all numeric sequences: We need to look at the total number of digits in any sequence (ignoring spaces) instead of just split chunks.
  • Better input handling: Use the input event instead of keyup — it covers paste, drag-and-drop, and other non-keyboard input methods.
  • Precise modification: Either truncate the sequence to 7 digits or obfuscate the excess, instead of randomly removing characters.

Option 1: Truncate Excess Digits

This version cuts off any numeric sequence longer than 7 digits, preserving the original spacing where possible:

$('#texto').on('input', function() {
  const $input = $(this);
  let inputValue = $input.val();

  // Process every numeric sequence (digits + optional spaces)
  const processedValue = inputValue.replace(/(\d[\d\s]*\d)/g, (match) => {
    // Remove spaces to count total digits
    const pureDigits = match.replace(/\s/g, '');
    if (pureDigits.length > 7) {
      // Keep first 7 digits, re-add spaces (optional: adjust spacing as needed)
      const truncatedDigits = pureDigits.substring(0, 7);
      // If you want to keep the original spacing pattern, you can adjust this logic
      return truncatedDigits.split('').join(' ');
    }
    return match;
  });

  // Update input value and styling
  if (processedValue !== inputValue) {
    $input.val(processedValue);
    $input.addClass("input_bloqueado");
  } else {
    $input.removeClass("input_bloqueado");
  }
});

Option 2: Obfuscate Excess Digits

If you prefer to obfuscate instead of truncate (e.g., keep first 3 digits, replace rest with *):

$('#texto').on('input', function() {
  const $input = $(this);
  let inputValue = $input.val();

  const processedValue = inputValue.replace(/(\d[\d\s]*\d)/g, (match) => {
    const pureDigits = match.replace(/\s/g, '');
    if (pureDigits.length > 7) {
      // Keep first 3 digits, obscure the rest
      const visibleDigits = pureDigits.substring(0, 3);
      const obscuredDigits = '*'.repeat(pureDigits.length - 3);
      return visibleDigits + obscuredDigits;
    }
    return match;
  });

  if (processedValue !== inputValue) {
    $input.val(processedValue);
    $input.addClass("input_bloqueado");
  } else {
    $input.removeClass("input_bloqueado");
  }
});

Why This Works

  • The regex (\d[\d\s]*\d) targets any sequence that starts and ends with a digit (including spaces in between), so it catches all valid numeric sequences you want to check.
  • By stripping spaces temporarily, we get the true total digit count for each sequence.
  • The input event ensures we handle every way a user can enter content, not just keyboard typing.

Original Script Limitations Addressed

  • Your original code only checked individual space-separated chunks, so it missed sequences where the total digits exceed 7 but no single chunk does.
  • The substring(0, num.length - 2) logic was unreliable — it randomly removed characters instead of targeting the excess digits.

内容的提问来源于stack exchange,提问作者sNniffer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:21:01