17

On Facebook, for example, if you have chosen the Arabic language for your keyboard, the textbox automatically gets RTL direction.

How can I implement this on my web application? I don't know the method or property used.

Abdullah Ilgaz
  • 719
  • 1
  • 17
  • 39
user217648
  • 3,338
  • 9
  • 37
  • 61

5 Answers5

33

You can use the CSS direction property to achieve this:

input{
  direction: rtl;
}

Update

To change the direction of the text dynamically based on the user input you can check the first character of input to see if it meets a given criteria, in this case a regular expression.

$('input').keyup(function(){
    $this = $(this);
    if($this.val().length == 1)
    {
        var x =  new RegExp("[\x00-\x80]+"); // is ascii

        //alert(x.test($this.val()));

        var isAscii = x.test($this.val());

        if(isAscii)
        {
            $this.css("direction", "ltr");
        }
        else
        {
            $this.css("direction", "rtl");
        }
    }

});

This is a basic example that uses ltr direction for ascii text and rtl for everything else.

Here's a working example.

Jamie Dixon
  • 53,019
  • 19
  • 125
  • 162
  • Thank you, Yes I know , but the direction will be rtl allways. what I want is when you write first character if it is english characters the input gets lrt direction and if you write arabic or Hebrew it changes automatically to rtl. – user217648 Oct 14 '11 at 16:08
  • thanks . but have not working in all swap change lang on keyboard – soheil bijavar Oct 12 '13 at 13:43
  • This solution is very good. +1 ...! Jut is has a small problem. When I press ``, this code thinks it is ascii and changes the direction to `ltr`. How can I fix it? – stack Dec 17 '15 at 23:10
  • Does it work for other languages such as Korean and Japanese? – Javid Apr 11 '17 at 19:55
30

In HTML5 you can simply do this:

<input type="text" dir="auto" />

this automatically change the direction of input base on first character. Hope it helps.

NOTE:

Edited: People say that it does not work in IE11.

Reza Ameri
  • 1,803
  • 3
  • 24
  • 32
  • 1
    It's not supported in IE 11 – Mohebifar Aug 27 '14 at 09:38
  • this is very fine, but how can I come to know the text direction on the server side – Ebrahim Oct 17 '15 at 15:18
  • There is also [`dirname` attribute](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fe-dirname) to send direction as separate field to server. IE / Edge might not support it yet: https://www.w3.org/International/tests/repository/html5/the-dirname-attribute/results-dirname (stale but expected as long as they [don't support dir=auto](https://wpdev.uservoice.com/forums/257854-microsoft-edge-developer/suggestions/10784463-support-dir-auto)) – Beni Cherniavsky-Paskin Aug 01 '18 at 11:36
1

it's best to detect text direction based on first character of the text. if the first one belongs to RTL language, then direction has to change.

example, a Persian text with English word in it.

به معنای واقعی tired هستم

another example, an English paragraph might contain a Persian word but the whole text has to be in LTR.

this word is used in different situations. the meaning of شیر is...

this function will check the first character typed in. if it belongs to a RTL language, then direction will change. This code supports all RTL languages.

function isRTL(str) {
  var letters = [];
  allRTL = new RegExp(
    "^[\u0590-\u05fe\u0600-۾܀-ݎݐ-ݾހ-\u07be߀-\u07fe\u0800-\u083e\u0840-\u085e\u08a0-\u08fe\u0900-ॾ]|\ud802[\udf60-\udf7e]+$"
  );
  var cursor = 1;
  for (var i = 0; i <= cursor; i++) {
    letters[i] = str.substring(i - 1, i);
    if(/\s/.test(letters[i])) {
      cursor++;
    }
    if (allRTL.test(letters[i])) {
      return true;
    }
  }
  return false;
}
var dir = $("input[type=text]");
dir.keyup(function(e) {
  if (isRTL(dir.val())) {
    $(this).css("direction", "rtl");
  } else {
    $(this).css("direction", "ltr");
  }
});

for more info visit this codepen.

Hadi Farnoud
  • 131
  • 1
  • 15
1

For input element you can use Unicode intrinsic direction

input {
    text-align: start;
    unicode-bidi: plaintext;
}

Of course, it works with other elements such as div with contenteditable as true as mentioned by comments.

realsarm
  • 583
  • 6
  • 11
-1

DISCLAIMER as stated by @bool3max: Many languages utilize Unicode characters and are written LTR. Your function sets direction: rtl for all non-ASCII characters, which isn't desired.

I have found also a Pen that does exactly the same without Regex. I have tweaked the code a bit to be compatible with multiple input fields and textareas. It serves the purpose of using it between; for example; EN and AR languages.

$('input[type="text"], textarea').each(function () {
    $(this).on('input keyup keypress', function () {
      if (isUnicode($(this).val())) {
        $(this).css('direction', 'rtl');
      } else {
        $(this).css('direction', 'ltr');
      }
    });
  });

  function isUnicode(str) {
    var letters = [];
    for (var i = 0; i <= str.length; i++) {
      letters[i] = str.substring((i - 1), i);
      if (letters[i].charCodeAt() > 255) { return true; }
    }
    return false;
  }
Tes3awy
  • 2,166
  • 5
  • 29
  • 51
  • 1
    *Many* languages utilize Unicode characters and are written LTR. Your function sets `direction: rtl` for all non-ASCII characters, which isn't desired. – bool3max Sep 25 '18 at 20:42