2

Here is my code:

$("body").on('input', 'textarea', function() {
  
      var el  = $(this);
      var len = el.val().length;
  
      if (len <= 1){
     var x = new RegExp("[A-Za-z]"); // is ascii
     var isAscii = x.test(el.val().substring(0, 1));
     if(isAscii){
      el.css("direction", "ltr");
     } else {
      el.css("direction", "rtl");
     }
    }
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>dynamic direction</textarea>

My current code changes the direction of such a textarea. It is based on the first character:

  • If the first character is either a Persian character or a sign, it sets rlt direction to that textarea.
  • If the first character is a English character, it sets lrt direction to that textarea.

Well that's not what I want. I want this:

  • If the first letter (not signs) is a English letter, then set the textarea ltr direction. Otherwise set it rtl.

Here is some examples:

var test = "..!";    // rtl
var test = "te";     // ltr
var test = "!te";    // ltr
var test = "..ق";    // rtl
var test = "مب";     // rtl
var test = "eس";     // ltr
var test = "سe";     // rtl
var test = "^سe";    // rtl
var test = ".32eس";  // ltr

How can I do that?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • What do you mean by "not signs"? – stratedge Aug 16 '16 at 02:48
  • @xjstratedgebx I mean there shoudn't be any different between `.....t` and `t`. – Martin AJ Aug 16 '16 at 02:49
  • Okay, so ignore all punctuation, then? Ignore periods, exclamation points, commas, questions marks, etc? – stratedge Aug 16 '16 at 02:50
  • @xjstratedgebx Yeah .. *signs* means `.`, `!`, `%`, `$`, `"` and etc .. Overall anything which isn't one of either English letters `[a-zA-Z]` or Persian letters `[آ-ی]`. – Martin AJ Aug 16 '16 at 02:51
  • try ^[.!?\\-]*[A-Za-z] as your regex on the whole input. It will still miss some cases. For example, what do you want to do with "! ? t". However, it might be a good start. – Karl Galvez Aug 16 '16 at 03:01
  • @KarlGalvez If the value of that textarea is `! ? t`, the direction should be `ltr`. Because the first letter is `t` which is English. – Martin AJ Aug 16 '16 at 03:02

3 Answers3

2

If I read the question correctly, the goal is to have the text read left-to-right if the first non-symbol/sign/punctuation character is an ASCII character, otherwise read right-to-left.

I think all you need to do is change your regex to first match 0 or more symbols/signs/punctuation-marks, and then to test if the next character is an ASCII character.

The regex [-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/] is a fairly complete regex for symbols/signs/punctuation-marks, found here: https://stackoverflow.com/a/8359631/4132627. You may need to add to it as you see fit.

Putting that together we'd get something like [-!$%^&*()_+|~=`{}\[\]:";'<>?,.\/]*[A-Za-z]. The * between the two character groups means "match 0 or more of the previous group".

I've updated your snippet with that regex and it appears to work as expected. Also removed the length check as this needs to run no matter how many characters there are.

This probably isn't perfect - there are many cases probably being left out. You may need to play with it a bit. For example, should that second character group also include numbers ([A-Za-z0-9])?

In any case, I hope this helps!

$("body").on('input', 'textarea', function() {
  
      var el  = $(this);
      var len = el.val().length;
  
      //if (len <= 1){
         var x = /^[-!$%^&*()_+|~=`{}\[\]:\";'<>?,.\/]*[A-Za-z]/; // is ascii
          var isAscii = x.test(el.val());

      if(isAscii){
       el.css("direction", "ltr");
      } else {
       el.css("direction", "rtl");
      }
     //}
  
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>dynamic direction</textarea>
Community
  • 1
  • 1
stratedge
  • 2,792
  • 17
  • 15
  • Seems correct .. thank you .. upvote .. Just you didn't define all signs in your character class like `@`. btw you can use this pattern instead: `/^[^a-zآ-ی]*[a-z]/i` – Martin AJ Aug 16 '16 at 03:25
  • Yeah, that negated match is probably better suited to your specific need, my answer was a little more generalized to illustrate matching punctuation prior to a letter. Your specific implementation will dictate the best course. If this answer works for you, I'd appreciate it if you'd mark it as the answer. – stratedge Aug 16 '16 at 03:31
1

You can have an array of persian, or any right to left, letters and check whether the first letter exists in the array using .inArray() function, something like this:

jsFiddle

var persianLetters = ['آ ', 'ا', 'ب', 'پ', 'ت', 'ث','ج', 'چ', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز' , 'ژ', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ک', 'گ' ,'ل', 'م', 'ن', 'و', 'ه', 'ى'];

$("#ta").on('input', function() {
  
    var el  = $(this);
    var txt = el.val();
    var len = txt.trim().length;
  
    if (len <= 1){
     var x = txt.substring(0, 1);

        // if the letter is not in the array, the $.inArray() will return -1          
        console.log($.inArray(x, persianLetters));
        
     if($.inArray(x, persianLetters) > -1){
           el.css("direction", "rtl");
        } else {
           el.css("direction", "ltr");
        }
    }      
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea name="txt" id="ta" cols="30" rows="10"></textarea>
Mi-Creativity
  • 9,554
  • 10
  • 38
  • 47
  • [Your slution doesn't work correcly](http://i.stack.imgur.com/YPAUX.png) – Martin AJ Aug 16 '16 at 03:20
  • I've tested it and it's working on copy/paste or direct input [example](http://image.prntscr.com/image/8a904001b93f48fd867ded99615bfe6a.png) – Mi-Creativity Aug 16 '16 at 03:24
  • Yes, I tested it by starting with dots .. It can be any signs *(like dots, question marks, etc)* .. btw this line `if (len <= 1){` doesn't let your script to check the other characters if the first one isn't a letter. – Martin AJ Aug 16 '16 at 03:27
  • anyway defining an array contains all Persian characters is a good idea .. +1 for that – Martin AJ Aug 16 '16 at 03:33
0

I use this utility CSS class, it automatically change the direction based on content preferred direction:

.direction-auto *,
.direction-auto {
    text-align: start;
    unicode-bidi: plaintext;
}
Amir2mi
  • 896
  • 9
  • 13