Simple Text box validation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • insomnia
    New Member
    • Mar 2008
    • 11

    Simple Text box validation

    Hi there a quick simple question, i need to be able to validate a textbox

    The user must enter a number within the range 0001 to 9999 only.

    And in the 4 number format XXXX , e.g. if they enter the number 11 i need it to be formated 0011.

    thanks
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Originally posted by insomnia
    Hi there a quick simple question, i need to be able to validate a textbox

    The user must enter a number within the range 0001 to 9999 only.

    And in the 4 number format XXXX , e.g. if they enter the number 11 i need it to be formated 0011.

    thanks
    Is this on a Windows form or on a Web page? If it's on a web page, do you need client side validation or server side validation?

    Comment

    • insomnia
      New Member
      • Mar 2008
      • 11

      #3
      Originally posted by balabaster
      Is this on a Windows form or on a Web page? If it's on a web page, do you need client side validation or server side validation?

      Sorry balabaster, it is on a vb.net form, i simply need to validate the data a user enters in to one of the fields on a form.

      the format must be numerical and 4 digits long.

      ----------

      and how could i disable a "space" being used

      eg. if the user types "two apples" rather than "twoapples"

      ---------

      cheers

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        vb.net form? Will presume that it is a windows application.

        You can use regex to do that
        this expression ^[0-9]{4}$ will limit the user to enter only digits into the textbox and that is limited to 4 digits in total.

        Comment

        • insomnia
          New Member
          • Mar 2008
          • 11

          #5
          Thanks all sorted :-)

          I used a combination of :

          Code:
          If Not IsNumeric(Textbox.Text) Or Textbox.Text = ""
          And :

          Code:
          Textbox.Text = Format(Convert.ToInt32(Textbox.Text), "0000")

          in the end to achieve my goal.

          cheers again

          Comment

          • todashah
            New Member
            • Feb 2008
            • 26

            #6
            Originally posted by insomnia
            Hi there a quick simple question, i need to be able to validate a textbox

            The user must enter a number within the range 0001 to 9999 only.

            And in the 4 number format XXXX , e.g. if they enter the number 11 i need it to be formated 0011.

            thanks
            To perform this kind of validation in C# write down following code in Leave event of textbox.

            int l = textBox1.Text.L ength;
            if (l != 4 || l < 0 || l > 9999)
            MessageBox.Show ("Invalid No");
            else
            MessageBox.Show ("Valid No");

            Make necessary changes in this code so it is execute in VB.net..
            Bye...Enjoy Coding

            Comment

            Working...