Monday, October 11, 2004

Function to Check special characters in text box in C#

//Copy and paste this function in the code behind file in C#.
//Call this function to check for special charecters in a text field.

private bool checkSpecialChars(string[] stringtochk)
{
bool nochkspchr = false; int i = 0; char[] ch = {'!','@','#','$','%','^','&','(',')','- ','_','+','=','\\','','{','}','[',']','?','<','>','.',',','~','`'};

while(i < stringtochk.Length)
{
if ((stringtochk[i].IndexOfAny(ch)) == -1)
nochkspchr = true;
else
{
MessageBox.Show("Cannot have special characters, Please enter valid characters [A-Z][a-z][0-9]","Configuration Manager",MessageBoxButtons.OK,MessageBoxIcon.Warning);
nochkspchr = false;
return false;
}
i++;
}
return nochkspchr;
}

No comments: