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?
Asked
Active
Viewed 4,346 times
8

Amir_P
- 8,322
- 5
- 43
- 92
-
Despite having different tags, this question is a possible duplicate of [Regex for check the input string is just in persian language](http://stackoverflow.com/questions/10561590/regex-for-check-the-input-string-is-just-in-persian-language) - since the only relevant part is the regular expression involved. – Geeky Guy May 13 '16 at 11:23
-
@Amir_P Can you paste your code pls? – Sergei Podlipaev May 13 '16 at 11:23
-
check in edit text have string A-z and a-z with help of Regx strings on textchangelistner – Nitesh Pareek May 13 '16 at 11:24
-
You can achieve this Using `Text Watcher`. – Jay Rathod May 13 '16 at 11:25
-
Also, don't check for the existence of English characters - check for the existence of any non-Persian character. 'ñ' is neither English nor Persian, for example. – Geeky Guy May 13 '16 at 11:26
-
refer https://detectlanguage.com/ – Saurabh Vardani May 13 '16 at 11:26
5 Answers
12
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.

User Learning
- 3,165
- 5
- 30
- 51
4
Persian characters are within the range: [\u0600-\u06FF]
. Validate your string with regex.

Sergei Podlipaev
- 1,331
- 1
- 14
- 34
4
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;

Jay Rathod
- 11,131
- 6
- 34
- 58
2
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";

Ahmad Aghazadeh
- 16,571
- 12
- 101
- 98
1
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]+$

Community
- 1
- 1

Debosmit Ray
- 5,228
- 2
- 27
- 43