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. .NET (Core and Framework)
  4. Textbox Validation

Textbox Validation

Scheduled Pinned Locked Moved .NET (Core and Framework)
7 Posts 4 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.
  • A Offline
    A Offline
    akosidandan
    wrote on last edited by
    #1

    Hello, I would like to ask if how do allow 2 input only in a textbox after entering decimal.

    M 1 Reply Last reply
    0
    • A akosidandan

      Hello, I would like to ask if how do allow 2 input only in a textbox after entering decimal.

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      use regular expression validator and in the validation expression specify this: (^\d*\.\d{2}$)

      Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      A 1 Reply Last reply
      0
      • M Manas Bhardwaj

        use regular expression validator and in the validation expression specify this: (^\d*\.\d{2}$)

        Manas Bhardwaj Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

        A Offline
        A Offline
        akosidandan
        wrote on last edited by
        #3

        Hello, Would you mine if could you give me some sample of using the format validation I have tried using lastindexof but my code doesn't work unless if I enter "." at begginning of my textbox. my code is this

        Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        TextBox1.TextChanged

            If TextBox1.Text.IndexOf(".") Then
                TextBox1.MaxLength = Val(TextBox1.TextLength) + 2
        
            End If
        

        End Sub

        Thanks, dfan23

        P 1 Reply Last reply
        0
        • A akosidandan

          Hello, Would you mine if could you give me some sample of using the format validation I have tried using lastindexof but my code doesn't work unless if I enter "." at begginning of my textbox. my code is this

          Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
          TextBox1.TextChanged

              If TextBox1.Text.IndexOf(".") Then
                  TextBox1.MaxLength = Val(TextBox1.TextLength) + 2
          
              End If
          

          End Sub

          Thanks, dfan23

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          He's already given you the regular expression you need to use. Why haven't you used that? The code you've got here isn't validation, it's input constraint - which is a different thing altogether. What happens if you put in .AA (hint - your code thinks it's valid, but it patently is wrong)?

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            He's already given you the regular expression you need to use. Why haven't you used that? The code you've got here isn't validation, it's input constraint - which is a different thing altogether. What happens if you put in .AA (hint - your code thinks it's valid, but it patently is wrong)?

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            A Offline
            A Offline
            akosidandan
            wrote on last edited by
            #5

            Hello, Can I ask again if there would there would be other way. I would like to ask also doesn't that the regular expression check if the input in textbox is as what the expression use? Thanks, dfan23

            L P 2 Replies Last reply
            0
            • A akosidandan

              Hello, Can I ask again if there would there would be other way. I would like to ask also doesn't that the regular expression check if the input in textbox is as what the expression use? Thanks, dfan23

              L Offline
              L Offline
              Laxman Auti
              wrote on last edited by
              #6

              I don't believe on restricting user for inputting. rather i will do it as following. This code will accept user decimal input and after user looses focus of this text box the validation logic will do the stuff for you.

                  private void textBox1\_Validating(object sender, CancelEventArgs e)
                  {
                      decimal result = 0.00M;
                      decimal.TryParse(textBox1.Text, out result);
                      textBox1.Text = decimal.Round(result, 2).ToString();            
                  }
              

              Let me know if you like this.

              wrote:

              Knock out 't' from can't, You can if you think you can :cool:

              1 Reply Last reply
              0
              • A akosidandan

                Hello, Can I ask again if there would there would be other way. I would like to ask also doesn't that the regular expression check if the input in textbox is as what the expression use? Thanks, dfan23

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                If you don't want to use a regular expression check, you could always use a masked edit box and set it to restrict the input.

                "WPF has many lovers. It's a veritable porn star!" - Josh Smith

                As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

                My blog | My articles | MoXAML PowerToys | Onyx

                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