User Control
-
I am creating a user control which will work as a Telephone number validator. I want that when Regex is not a match the Text property should not return any kind of string. Can someone help with the code, as with my code, the errorprovider is instantiated but it still continues and returns the invalid phone number
public override string Text
{
get {
string phone = textBox1.Text.Trim() + textBox2.Text.Trim() + textBox3.Text.Trim();
if (!Regex.IsMatch(phone, @"^\d{9}$"))
{
errorProvider1.SetError(textBox3, "Please provide a valid phone number");
errorProvider1.SetIconAlignment(textBox3, ErrorIconAlignment.MiddleRight);} return phone; } set { string phone; phone = value; textBox1.Text = phone.Substring(0, 3); textBox2.Text = phone.Substring(3, 3); textBox3.Text = phone.Substring(6); } }
-
I am creating a user control which will work as a Telephone number validator. I want that when Regex is not a match the Text property should not return any kind of string. Can someone help with the code, as with my code, the errorprovider is instantiated but it still continues and returns the invalid phone number
public override string Text
{
get {
string phone = textBox1.Text.Trim() + textBox2.Text.Trim() + textBox3.Text.Trim();
if (!Regex.IsMatch(phone, @"^\d{9}$"))
{
errorProvider1.SetError(textBox3, "Please provide a valid phone number");
errorProvider1.SetIconAlignment(textBox3, ErrorIconAlignment.MiddleRight);} return phone; } set { string phone; phone = value; textBox1.Text = phone.Substring(0, 3); textBox2.Text = phone.Substring(3, 3); textBox3.Text = phone.Substring(6); } }
how about putting a
return string.Empty;
inside the if-rejected? BTW: Textbox.Text typically doesn't return null, when it is empty it gives an empty string. So I don't think bad content should return null; you could make it throw an exception though. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.
-
I am creating a user control which will work as a Telephone number validator. I want that when Regex is not a match the Text property should not return any kind of string. Can someone help with the code, as with my code, the errorprovider is instantiated but it still continues and returns the invalid phone number
public override string Text
{
get {
string phone = textBox1.Text.Trim() + textBox2.Text.Trim() + textBox3.Text.Trim();
if (!Regex.IsMatch(phone, @"^\d{9}$"))
{
errorProvider1.SetError(textBox3, "Please provide a valid phone number");
errorProvider1.SetIconAlignment(textBox3, ErrorIconAlignment.MiddleRight);} return phone; } set { string phone; phone = value; textBox1.Text = phone.Substring(0, 3); textBox2.Text = phone.Substring(3, 3); textBox3.Text = phone.Substring(6); } }
I agree with Luc that a property get should not return a null, but just as a suggestion have you considered using a MaskedTextBox instead of the three textboxes - it would probably be more obvious and friendly for the user. It would also remove the need for a regex check at all. You can turn off the underline prompt with
MaskedTextBox.PromptChar = " ";
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
-
I agree with Luc that a property get should not return a null, but just as a suggestion have you considered using a MaskedTextBox instead of the three textboxes - it would probably be more obvious and friendly for the user. It would also remove the need for a regex check at all. You can turn off the underline prompt with
MaskedTextBox.PromptChar = " ";
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
I did try the Masked the text box, with the
MaskedTextBox.PromptChar = " "
option but the problem was the two brackets in the text box for the phone area code
( )
were too ugly and wrinkly. And also when you click on the text box the cursor is not inside the"( )"
brackets it gets placed before the first bracket, which was kind of irritating. So I though let me try out the UserControl text box option. Which is working fine now.