I'd like to restrict my form input from entering non-english characters. For example, all Chinese, Japanese, Cyrllic, but also single characters like: à, â, ù, û, ü, ô, î, ê. Would this be possible? Do I have to set up a locale on my MVC application or rather just do a regex textbox validation? Just a side note, I want to be able to enter numbers and other characters. I only want this to exclude letters.
Please advice, thank you
For this you have to use Unicode character properties and blocks. Each Unicode code points has assigned some properties, e.g. this point is a Letter. Blocks are code point ranges.
For more details, see:
regular-expressions.info for some general information about Unicode code points, character properties, scripts and blocks
MSDN for the supported properties and blocks in .net
Those Unicode Properties and blocks are written \p{Name}
, where "Name" is the name of the property or block.
When it is an uppercase "P" like this \P{Name}
, then it is the negation of the property/block, i.e. it matches anything else.
There are e.g. some properties (only a short excerpt):
There are e.g. some blocks (only a short excerpt):
What I used in the solution:
\P{L}
is a character property that is matching any character that is not a letter ("L" for Letter)
\p{IsBasicLatin}
is a Unicode block that matches the code points 0000 - 007F
So your regex would be:
^[\P{L}\p{IsBasicLatin}]+$
In plain words:
This matches a string from the start to the end (^
and $
), When there are (at least one) only non letters or characters from the ASCII table (doce points 0000 - 007F)
A short c# test method:
string[] myStrings = { "Foobar",
"Foo@bar!\"§$%&/()",
"Föobar",
"fóÓè"
};
Regex reg = new Regex(@"^[\P{L}\p{IsBasicLatin}]+$");
foreach (string str in myStrings) {
Match result = reg.Match(str);
if (result.Success)
Console.Out.WriteLine("matched ==> " + str);
else
Console.Out.WriteLine("failed ==> " + str);
}
Console.ReadLine();
Prints:
matched ==> Foobar
matched ==> Foo@bar!\"§$%&/()
failed ==> Föobar
failed ==> fóÓè
You can use a Regular Expression attribute on your ViewModel to restrict that
public class MyViewModel
{
[System.ComponentModel.DataAnnotations.RegularExpression("[a-zA-Z]+")]
public string MyEntry
{
get;
set;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With