Wednesday, April 6, 2011

Validation of TextBox

using System.Text.RegularExpressions;

if (ValidateTextBox(txtabc.Text) == "")
{
//Do Something
}
else
{
lblMessage.Text = "Please enter Valid Text";
}


//Function
//Validation for the input start with alphabet
//Number can be added at last or middle
//Only _ is allowed in the string without any special character
private string ValidateTextBox(string strInput)
{
string strValid = "";
string strPattern = @"^[a-zA-Z]{1,20}[0-9a-zA-Z_]*$";
Match objMatch = Regex.Match(strInput, strPattern);
if (!objMatch.Success)
{
strValid = "Something";
}
return strValid;
}

1 comment:

Anonymous said...

Very helpful

Thanks by sajith