Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. User Control

User Control

Scheduled Pinned Locked Moved C#
regexhelp
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    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);
            }
        }
    
    L OriginalGriffO 2 Replies Last reply
    0
    • H humayunlalzad

      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);
              }
          }
      
      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • H humayunlalzad

        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);
                }
            }
        
        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        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 have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        H 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          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.

          H Offline
          H Offline
          humayunlalzad
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups