Reputation: 59
I want a textbox to accept just Persian alphabet, and don't accept any signs in C#. Can anyone help me with code?
Upvotes: 0
Views: 1135
Reputation: 19149
You must add event for your textbox. What you want is KeyPress Event.
see this tutorial in DotNet Perls.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !e.KeyChar <= 'BiggestPersianChar' && !e.KeyChar >= 'SmallesPersianChar';
}
Currently i dont know the Biggest persian char and smallest one. but i hope you know it.
Edit:
I Guess that these are biggest and smallest char. by Big and small i mean its Unicode.
e.Handled = !e.KeyChar <= 'ی' && !e.KeyChar >= 'ا';
Upvotes: 1
Reputation: 4798
This is probably what you're looking for:
// Only allows "Persian characters" and "Space".
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Regex.IsMatch(e.KeyChar.ToString(), @"\p{IsArabic}")
&& !string.IsNullOrWhiteSpace(e.KeyChar.ToString()))
e.Handled = true;
}
// Only allows "Persian characters", "Space" and "Numbers".
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Regex.IsMatch(e.KeyChar.ToString(), @"\p{IsArabic}")
&& !string.IsNullOrWhiteSpace(e.KeyChar.ToString())
&& !char.IsDigit(e.KeyChar))
e.Handled = true;
}
Specific character sets in the Unicode standard occupy a specific range or block of consecutive code points. For example, the basic Latin character set is found from \u0000
through \u007F
, while the Arabic character set is found from \u0600
through \u06FF
.
The regular expression construct
\p{ name }
matches any character that belongs to a Unicode general category or named block.
You can read more about Unicode Block here.
Upvotes: 3