How can I automatically set text direction based on input text language?

Add dir=auto to your input elements:

Use dir="auto" on forms and inserted text in order to automatically detect the direction of content supplied at run-time.

https://www.w3.org/International/questions/qa-html-dir#quickanswer


There's also Twitter's library, which may help:

https://github.com/twitter/RTLtextarea


I wrote a JQuery code to do this. Just add class "checkRTL" to the element you want to set its direction.

var rtlChar = /[\u0590-\u083F]|[\u08A0-\u08FF]|[\uFB1D-\uFDFF]|[\uFE70-\uFEFF]/mg;
$(document).ready(function(){
    $('.checkRTL').keyup(function(){
        var isRTL = this.value.match(rtlChar);
        if(isRTL !== null) {
            this.style.direction = 'rtl';
         }
         else {
            this.style.direction = 'ltr';
         }
    });
});