2

I’m working on a data entry project(visual studio windowsform) and there are two main languages that the datas have to entered in, english and arabic, i want some fields to show errorprovider if the user enters in English language in an arabic field and vice versa, is that possible? Thanks.

AIDEN IV
  • 79
  • 1
  • 9
  • It should be clear enough, when you write in the wrong direction. Anyway, arabic symbols occupy specific CodePoints in the Unicode [BMP](https://en.wikipedia.org/wiki/Plane_(Unicode)#Basic_Multilingual_Plane). See the characters ranges dedicated to Arabic writing in Unicode: [Arabic script](https://en.wikipedia.org/wiki/Arabic_script) (top-right of the page). English/Latin scripts use different CodePoints (different Unicode *numbers*). – Jimi Jun 06 '19 at 12:16

3 Answers3

0

You can do it by yourself writing logical if condition on keystroke checking for whether entered letter is of English alphabet or not. But this is not perfect solution,it wont work for other language.

jitendra
  • 1
  • 3
0

Just check if all letters in the entered text are part of the english alphabet.

  string text = "abc";

  char[] englishAlphabet = new char[] { '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
  bool english = true;
  foreach (char c in text.ToLower())
    if (!englishAlphabet.Contains(c))
    {
      english = false;
      break;
    }

  if (english)
    // Do some stuff
  else
    // Show error

Same for the arabic alphabet.

Owns_Rekt
  • 164
  • 11
0

You can build a function to check for arabic characters using regex:

internal bool HasArabicCharacters(string text)
{

  Regex regex = new Regex(

    "^[\u0600-\u06FF]+$");

  return regex.IsMatch(text);
}

Or you can build a function for english characters too using regex:

internal bool HasEnglishCharacters(string text)
{

      Regex regex = new Regex(

        "^[a-zA-Z0-9]*$");

      return regex.IsMatch(text);
}

Source: This question

And after that you can do something like this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
  if(HasArabicCharacters(textBox1.Text) == true)
  {
    //have arabic chars
    //delete text for example
  }
  else
  {
    //don't have arabic chars
  }
}

Output:

؋ = return true;
a = return false;
ئ = return true;
Mikev
  • 2,012
  • 1
  • 15
  • 27