Home  >  Q&A  >  body text

Methods that no longer allow right-to-left text input

<p>How to prevent or at least detect right to left text input (like Arabic or Hebrew) in an input box? </p>
P粉021854777P粉021854777432 days ago467

reply all(1)I'll reply

  • P粉541796322

    P粉5417963222023-08-16 10:30:41

    You can try to use the dir attribute of the input tag to specify the text direction.

    <input type="text" dir="ltr" placeholder="在此输入...">

    If this doesn't solve your problem, you can use the javascript interactive check snippet below to do it.

    <input type="text" id="textInput" placeholder="在此输入...">
    <div id="result"></div>
    
    <script>
    const inputElement = document.getElementById('textInput');
    const resultElement = document.getElementById('result');
    
    inputElement.addEventListener('input', function() {
      const text = inputElement.value;
      const rtlRegex = /[\u0600-\u06FF\u0750-\u077F\u0590-\u05FF]/;
    
      if (rtlRegex.test(text)) {
        resultElement.textContent = '检测到从右到左的文本。';
      } else {
        resultElement.textContent = '未检测到从右到左的文本。';
      }
    });
    </script>

    Also check if there are any other CSS rules affecting your input tag.

    reply
    0
  • Cancelreply