Community discussion forum

c#+ textbox values validation

  • 8 months ago

    hi friends ,
    how can i validate the values entered in textbox in a windows form. i want the validation code for  phone number, text dateand time and email address, please give the answer clearly because i m  new to c#.

    thanks in advance
    Radhika

  • Advertisement

    Simply the fastest line-level profiler for .NET ever

    “The low overhead means it has minimal impact on the execution of my program”
    Mark Everest, Development Team Leader, Renault F1 Team Ltd.

    Try out the new ANTS Profiler 4 for yourself. Download your 14-day trial now

  • 8 months ago

    hi radhikabista,
    You can easily validate the values entered in textbox in a windows form  using Regualr Expressions (Regex). Just checkout the given regex code for your requirements.

    1. Regex for Date:- (?<Month>\d{1,2})/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))
    2. Regex for Telephone Number:- \((?<AreaCode>\d{3})\)\s*(?<Number>\d{3}(?:-|\s*)\d{4})
    3. Regex for Email ID:- ([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})

      Replace Regex Equation in the given code for to validate against it.

      private void textBox1_Validating(object sender, CancelEventArgs e)
      {
          Regex regex = new Regex("(?<Month>\d{1,2})/(?<Day>\d{1,2})/(?<Year>(?:\d{4}|\d{2}))");
          if (regex.IsMatch(textBox1.Text))
          {
              errorProvider1.SetError(textBox1, String.Empty);
          }
          else
          {
              errorProvider1.SetError(textBox1,
                    "This is not valid date");
          }
      }

      Hope this will help you.
      Thanks & Regards
      Dilipv
  • 8 months ago

    thanks dilip for yur reply,

    but i dont know anything about regix expression and how to use it. i tried to use the code as yur example but during compilation it says unexpected symbol. do i need to include some library. how and where to write the validation codes.

     

    private void text_customer_email_Validating(object sender, CancelEventArgs e)
    {
        Regex regex = new 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 (regex.IsMatch(text_customer_email.Text))
        {
            errorProvider1.SetError(text_customer_email, String.Empty);
        }
        else
        {
            errorProvider1.SetError(text_customer_email,
                  "This is not valid email");
        }
    }
           

    Regards

    Radhika 

     

  • 8 months ago

    hi radhikabista,
    For regular expressions you need to include following package.

    using System.Text.RegularExpressions;

    [quote user="radhikabista"]Regex regex = new 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})");
    [/quote]

    Sorry Radhika, it's my fault. Since code is in c#  but Regex is for VB. Just change your code in quote as follows. You need to put '@' sign at the start of the Regex expression. Due to '@' sign it will become compatible for c# also.

    Regex
    regex = new 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})");

    A regular expression (regex or regexp for short) is a special text string for describing a search pattern. You can think of regular expressions as wildcards on steroids. You are probably familiar with wildcard notations such as *.txt to find all text files in a file manager.
    For detailed study OR software to download, just checkout the given link. It contains set of Regex pattern for search strings.

    http://www.ultrapico.com/Expresso.htm 

    Hope it will help you.
    Thanks & Regards
    Dilipv




  • 8 months ago

    thanks very much for yur kind reply.

    i tried adding @  but still it says

    "the name error provider1 does not exists in current context ".

    now i have installed Expresso. i m able to generate the expressions but i couldnot save the expression to library. and i have no idea hoe to use these expression libraries to  C#.code.

    i tried 

    using System.Text.RegularExpressions;

    private void text_customer_phone_Validating(object sender, EventArgs e)
     {


              Regex regex = new Regex(@"\{9,13}");
        if (regex.IsMatch(text_customer_phone.Text))
        {
            errorProvider1.SetError(text_customer_phone, String.Empty);
        }
        else
        {
            errorProvider1.SetError(text_customer_phone,
                  "This is not valid phone number.");
        }
        if (regex.IsMatch(text_customer_phone.Text))
        {
            errorProvider1.SetError(text_customer_phone, String.Empty);
        }
        else
        {
            errorProvider1.SetError(text_customer_phone,
                  "This is not valid phone");
        }
            }

    Regards

    Radhika 

     

     

     

     

     

     

  • 2 months ago

    hmm!

    u need to add a component name 'errorProvider' from component tab. and athe component tab found in toolbox.

    You may need to call the " text_customer_phone_Validating(object sender, EventArgs e) " as

    text_customer_phone_Validating(null, null) from action point.

    The result is if the format match with textbox then no blink else a redExclamation icon blink beside the textbox.

    you may need to apply ur logic to get result what u want..

    B/C

Post a reply

Enter your message below

Sign in or Join us (it's free).