Monday, January 4, 2010

URL validation in C# window application

private void TextBox1_Validating(object sender, CancelEventArgs e)
{
System.Text.RegularExpressions.Regex rURL = new System.Text.RegularExpressions.Regex(@"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
if (txtURL.Text.Length > 0)
{
if (!rURL.IsMatch(txtEmail.Text))
{
MessageBox.Show("Please provide valid URL", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtURL.SelectAll();
}
}
}

No comments: