c#+ textbox values validation

  • 13 years 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

  • 13 years 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
  • 13 years 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 

     

  • 13 years 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




  • 13 years 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 

     

     

     

     

     

     

  • 12 years 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).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Anyone who considers arithmetic methods of producing random digits is, of course, in a state of sin.” - John von Neumann