开发者

How can I validate hebrew string input in C#?

I have a textbox and I want to validate that the user enters only hebrew stri开发者_如何转开发ngs. How can I do that?


The obvious approach is to check the codepoints with TextBox.Text.ToCharArray(). Hebrew glyphs are codepoints 0x0580 through 0x05ff with supplementals 0xfb1d through 0xfb4f. Plus the arabic digits.

Not being a native speaker, I would however assume that Latin characters can appear when spelling trade mark names, foreign words and acronyms. Note the use of "RSS" in this page. Which put a pretty big hole in any attempt to verify the text.


avoid unicode chars in code

private const char FirstHebChar = (char)1488; //א
private const char LastHebChar = (char)1514; //ת
private static bool IsHebrew(this char c)
{
     return c >= FirstHebChar && c <= LastHebChar;
}

see http://blogs.microsoft.co.il/shimmy/2012/02/03/determine-if-char-is-hebrew-2/


One way to do it would be to go grab the ASP.NET AJAX Control Toolkit @ http://www.asp.net/ajax/ajaxcontroltoolkit/samples/

Use the FilteredTextBox control (basically an extension of the textbox).

Keep in mind this only will prevent them from submitting the form if it has non-Hebrew strings. It won't stop them from entering them in the text field.

But I imagine this is going to create a different problem. You'll have to define what other characters are allowed, punctuation, numbers, etc.

What problem are you trying to solve? Perhaps we might be able to come up with something else.


Use a regular expression that ensures the text is in a particular range. You'll probably want to add the space character, as well as any R-to-L and L-R characters that are embedded.


If you are doing it with an ASP.NET web app, then you can use the RegularExpressionValidator control with Unicode escape characters to create your RegEx match for valid strings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜