Amir Panahandeh
Amir Panahandeh

Reputation: 9049

How to detect if edittext has English characters

I want to detect if my edittext has English characters and notify user to change it because i want just Persian characters for name and family name. how can i filter edittext to accept just Persian characters or detect English characters and show an error?

Upvotes: 8

Views: 4610

Answers (5)

Ahmad Aghazadeh
Ahmad Aghazadeh

Reputation: 17131

public final static String PERSIAN_STRING = "ا آ ب پ ت ث ج چ ح خ د ذ ر ز ژ س ش ص ض ط ظ ع غ ف ق ک گ ل م ن و ه ی";
public final static String LATIN_STRING = "a b c d e f g h i j k l m n o p q r s t u v w x y z";

 if (PERSIAN_STRING.contains(str.toLowerCase()))
     lang = "PERSIAN";

  if (LATIN_STRING.contains(str.toLowerCase()))
      lang = "LATIN";

Upvotes: 2

Jay Rathod
Jay Rathod

Reputation: 11255

Try the following regular expression Persian characters range.

public static final Pattern RTL_CHARACTERS = 
    Pattern.compile("[\u0600-\u06FF\u0750-\u077F\u0590-\u05FF\uFE70-\uFEFF]");
Matcher matcher = RTL_CHARACTERS.matcher("Texts");
if(matcher.find()){
   return true;  // it's RTL
} 

Or Use this method.

public static boolean textPersian(String s) {
for (int i = 0; i < Character.codePointCount(s, 0, s.length()); i++) {
    int c = s.codePointAt(i);
    if (c >= 0x0600 && c <=0x06FF || c== 0xFB8A || c==0x067E || c==0x0686 || c==0x06AF)
        return true;
}
return false;

Upvotes: 4

Debosmit Ray
Debosmit Ray

Reputation: 5413

Summarizing a couple things here. Since, @Sergei Podlipaev has noted that Persian characters are in the range \u0600 to \u06FF, you can use regex. I am not sure if I am specifying the bounds correctly, but this will give you an idea.

To see if english character set is used - ^[a-zA-Z0-9]*$
To see if Persian character set is used - ^[\u0600-\u06FF]+$

Upvotes: 1

User Learning
User Learning

Reputation: 3513

I think its not easy to detect and inform user to instruct. Simply u can do something like this. In XML file:

<EditText
    android:id="@+id/LanguageText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:digits="@string/app_lan" />

In Strings.xml :

<string name="app_lan">YOUR LANGUAGE</string>

Example If English :

<string name="app_lan">abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ</string>

For ur language same like this.

Upvotes: 12

Sergei Podlipaev
Sergei Podlipaev

Reputation: 1421

Persian characters are within the range: [\u0600-\u06FF]. Validate your string with regex.

Upvotes: 4

Related Questions