Hi,
Regex rx = new Regex("^[a-zA-Z0-9_]*$");
<pre lang="cs">if (rx.IsMatch(UserName.Text))
{
Valid = true;
}
else
{
MessageBox.Show("Special characters cannot be submitted as a part of the Username (except Underscore).\nPlease re enter the User name and click Button.");
UserName.Text = "";
UserName.Focus();
return;
}
try this code, it may helpful for you