Form validation using regular expressions
Date: 11/20/04
(C Sharp) Keywords: no keywords
Hi, I want to validate the string for the "First Name" field in a windows application.
Here's the code I wrote,
public static bool firstname(TextBox box)
{
bool correct = true;
if ( !Regex.Match( box.Text,
@"^[A-Z]+([a-zA-Z]+|[a-zA-Z]+\s[a-zA-Z]+)*$" ).Success )
{
correct = false;
}
else
correct = true;
updateControlColor(box, correct);
return correct;
}
Everything works, except it doesn't allow "-" (hyphens) in the field. What's the
correct way to write the regex? (and hyphens can only follow a word, not the
whitespace).
Also, I am binding an ErrorProvider object to the form, how can I display the
error message onto the windows application? (Right now, the error message
only shows up when I mouse-over the error icon).
Thanks in advance.
Source: http://www.livejournal.com/community/csharp/21648.html