how to prevent non-English characters and allow non-alpha characters

I have a string, and I want to make sure that every letter in it is English. The other characters, I don't care.

  1. 34556#%42%$23$%^*&sdfsfr - valid
  2. 34556#%42%$23$%^*&בלה בלה - not valid

Can I do that with Linq? RegEx?

Thanks

Answers


Maybe you could use

using System.Linq;

...

static bool IsValid(string str)
{
  return str.All(c => c <= sbyte.MaxValue);
}

This considers all ASCII chars to be "valid" (even control characters). But punctuation and other special characters outside ASCII are not "valid". If str is null, an exception is thrown.


You can define in a character class either all characters/character ranges/Unicode-properties/blocks you want to allow or you don't want to allow.

[abc] is a character class that allows a and b and c

[^abc] is a negated character class that matches everything but not a or b or c

Here in your case I would go this way, no need to define every character:

^[\P{L}A-Za-z]*$

Match from the start to the end of the string everything that is not a letter [^\p{L}] or A-Za-z.

\p{L} Is a Unicode property and matches everything that has the property letter. \P{L} is the negated version, everything that is not a letter.

Test code:

string[] StrInputNumber = { "34556#%42%$23$%^*&sdfsfr", "asdf!\"§$%&/()=?*+~#'", "34556#%42%$23$%^*&בלה בלה", "öäü!\"§$%&/()=?*+~#'" };
Regex ASCIILettersOnly = new Regex(@"^[\P{L}A-Za-z]*$");
foreach (String item in StrInputNumber) {

    if (ASCIILettersOnly.IsMatch(item)) {
        Console.WriteLine(item + " ==> Contains only ASCII letters");
    }
    else {
        Console.WriteLine(item + " ==> Contains non ASCII letters");

    }
}

Some more basic regex explanations: What absolutely every Programmer should know about regular expressions


One thing you can try is put the char you want in this regx

bool IsValid(string input) {     
  return !(Regex.IsMatch(@"[^A-Za-z0-9'\.&@:?!()$#^]", input)); 
}

char other than specfied in the regx string are get ignored i.e return false..


Need Your Help

How to run multiple emulators from shell?

shell android-emulator

In order to run one emulator, I use command

Lookup for access list value from another site in sharepoint

sharepoint

Look up for getting one list value from one site to another site