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. Visual Basic
  4. Numerical textbox validation, how to handle an empty string? [modified]

Numerical textbox validation, how to handle an empty string? [modified]

Scheduled Pinned Locked Moved Visual Basic
comregexhelptutorialquestion
7 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.
  • M Offline
    M Offline
    MatthysDT
    wrote on last edited by
    #1

    I want to validate that my textbox contains a numerical value. I do this in the "textbox.validating" event, everything works fine until all the text is cleared and the box has an empty string. The validation doesn't allow an empty string because it's not numerical (no matter how I code it). But this field is not compulsory for the user to enter, so he should be able to enter either a numerical value or an empty string. Here is my code so far, it doesn't work for empty strings, textbox remains invalidated and forces focus onto the control.

        Private Sub MyMaskedTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyMaskedTextBox.Validating
            Dim num As Double
            If Me.MyMaskedTextBox.Text = "" Or IsDBNull(Me.MyMaskedTextBox.Text) Then
                MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
            Else
                Try
                    num = Me.MyMaskedTextBox.Text
                    MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
                Catch ex As Exception
                    MyErrorProvider.SetError(Me.MyMaskedTextBox, "Value must be numeric")
                End Try
            End If
    
        End Sub
    

    This is a real problem because nothing I've tried so far allows the user to enter an empty string! Can't I somehow force the field to validate even if the contents don't match the validation criteria?

    _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit "Watching Migthy Joe Young made me hate my life..................................I want a gorilla!" A. Havemann

    S C M 3 Replies Last reply
    0
    • M MatthysDT

      I want to validate that my textbox contains a numerical value. I do this in the "textbox.validating" event, everything works fine until all the text is cleared and the box has an empty string. The validation doesn't allow an empty string because it's not numerical (no matter how I code it). But this field is not compulsory for the user to enter, so he should be able to enter either a numerical value or an empty string. Here is my code so far, it doesn't work for empty strings, textbox remains invalidated and forces focus onto the control.

          Private Sub MyMaskedTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyMaskedTextBox.Validating
              Dim num As Double
              If Me.MyMaskedTextBox.Text = "" Or IsDBNull(Me.MyMaskedTextBox.Text) Then
                  MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
              Else
                  Try
                      num = Me.MyMaskedTextBox.Text
                      MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
                  Catch ex As Exception
                      MyErrorProvider.SetError(Me.MyMaskedTextBox, "Value must be numeric")
                  End Try
              End If
      
          End Sub
      

      This is a real problem because nothing I've tried so far allows the user to enter an empty string! Can't I somehow force the field to validate even if the contents don't match the validation criteria?

      _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit "Watching Migthy Joe Young made me hate my life..................................I want a gorilla!" A. Havemann

      S Offline
      S Offline
      SHatchard
      wrote on last edited by
      #2

      The following code may be better if string.isnullorempty(Me.MyMaskedTextBox.Text) orelse isnumeric(Me.MyMaskedTextBox.Text) then 'Its a number or an empty string else 'Its got content but is not numeric end if

      M 2 Replies Last reply
      0
      • S SHatchard

        The following code may be better if string.isnullorempty(Me.MyMaskedTextBox.Text) orelse isnumeric(Me.MyMaskedTextBox.Text) then 'Its a number or an empty string else 'Its got content but is not numeric end if

        M Offline
        M Offline
        MatthysDT
        wrote on last edited by
        #3

        Thank you SHatchard but that code does exactly the same as mine. The textbox still doesn't validate with an empty string and focus is forced to the control until it validates.

        _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit "Watching Migthy Joe Young made me hate my life..................................I want a gorilla!" A. Havemann

        1 Reply Last reply
        0
        • S SHatchard

          The following code may be better if string.isnullorempty(Me.MyMaskedTextBox.Text) orelse isnumeric(Me.MyMaskedTextBox.Text) then 'Its a number or an empty string else 'Its got content but is not numeric end if

          M Offline
          M Offline
          MatthysDT
          wrote on last edited by
          #4

          This field is databounded to a DB cloumn of the type Double. It seems that NULL is not accepted by datasets for Double columns. Eventhough the default value is DBNull (which .Net accepts) and the AllowDBNull property is set to TRUE (which .Net accepts) The columns NullValue property can only be set to , .Net rejects all other selections. I still don't have a solution but this problem starting to look more like a bug in .Net than and not in my code.

          _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit "Watching Migthy Joe Young made me hate my life..................................I want a gorilla!" A. Havemann

          1 Reply Last reply
          0
          • M MatthysDT

            I want to validate that my textbox contains a numerical value. I do this in the "textbox.validating" event, everything works fine until all the text is cleared and the box has an empty string. The validation doesn't allow an empty string because it's not numerical (no matter how I code it). But this field is not compulsory for the user to enter, so he should be able to enter either a numerical value or an empty string. Here is my code so far, it doesn't work for empty strings, textbox remains invalidated and forces focus onto the control.

                Private Sub MyMaskedTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyMaskedTextBox.Validating
                    Dim num As Double
                    If Me.MyMaskedTextBox.Text = "" Or IsDBNull(Me.MyMaskedTextBox.Text) Then
                        MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
                    Else
                        Try
                            num = Me.MyMaskedTextBox.Text
                            MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
                        Catch ex As Exception
                            MyErrorProvider.SetError(Me.MyMaskedTextBox, "Value must be numeric")
                        End Try
                    End If
            
                End Sub
            

            This is a real problem because nothing I've tried so far allows the user to enter an empty string! Can't I somehow force the field to validate even if the contents don't match the validation criteria?

            _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit "Watching Migthy Joe Young made me hate my life..................................I want a gorilla!" A. Havemann

            C Offline
            C Offline
            cutequencher
            wrote on last edited by
            #5

            try to replace --If Me.MyMaskedTextBox.Text = "" Or IsDBNull(Me.MyMaskedTextBox.Text) Then with --if isnothing(Me.MyMaskedTextBox.Text) then or --if Me.MyMaskedTextBox.Text is nothing then if it still doesnt work... may i know what vb are you using?

            M 1 Reply Last reply
            0
            • C cutequencher

              try to replace --If Me.MyMaskedTextBox.Text = "" Or IsDBNull(Me.MyMaskedTextBox.Text) Then with --if isnothing(Me.MyMaskedTextBox.Text) then or --if Me.MyMaskedTextBox.Text is nothing then if it still doesnt work... may i know what vb are you using?

              M Offline
              M Offline
              MatthysDT
              wrote on last edited by
              #6

              Thank you for the response! That doesn't work either, I'm using VB .Net in Visual Studio 2005. That field is data-binded to a dataset, the column's type is Double. I think that has something to do with it not allowing Nulls. Funny thing is, it allows NULLS, just not when you try to enter it with the textbox!

              _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit

              1 Reply Last reply
              0
              • M MatthysDT

                I want to validate that my textbox contains a numerical value. I do this in the "textbox.validating" event, everything works fine until all the text is cleared and the box has an empty string. The validation doesn't allow an empty string because it's not numerical (no matter how I code it). But this field is not compulsory for the user to enter, so he should be able to enter either a numerical value or an empty string. Here is my code so far, it doesn't work for empty strings, textbox remains invalidated and forces focus onto the control.

                    Private Sub MyMaskedTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyMaskedTextBox.Validating
                        Dim num As Double
                        If Me.MyMaskedTextBox.Text = "" Or IsDBNull(Me.MyMaskedTextBox.Text) Then
                            MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
                        Else
                            Try
                                num = Me.MyMaskedTextBox.Text
                                MyErrorProvider.SetError(Me.MyMaskedTextBox, "")
                            Catch ex As Exception
                                MyErrorProvider.SetError(Me.MyMaskedTextBox, "Value must be numeric")
                            End Try
                        End If
                
                    End Sub
                

                This is a real problem because nothing I've tried so far allows the user to enter an empty string! Can't I somehow force the field to validate even if the contents don't match the validation criteria?

                _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit "Watching Migthy Joe Young made me hate my life..................................I want a gorilla!" A. Havemann

                M Offline
                M Offline
                MatthysDT
                wrote on last edited by
                #7

                O.k, I found a work-around for the problem. Posting it here for the sake of anyone having similar problems. What happens that causes the problem is that VS doesn't allow a control (in my case a textbox) to lose focus if the text inside it doesn't validate. But for numerical textboxes it also doesn't validate empty strings, even if you want the user to be able to add an empty string. The solution is to set the data source update mode to never, this disables .Net validation on the control, allowing you to do this with custom code. Me.MyNumericTextBox.DataBindings.DefaultDataSourceUpdateMode = DataSourceUpdateMode.Never The validating event still fires though, but it's under YOUR control now, so the following code will allow you to force the user to enter either a numerical value, or an EMPTY STRING. Private Sub MyNumericTextBox_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyNumericTextBox.Validating If String.IsNullOrEmpty(Me.MyNumericTextBox.Text) OrElse IsNumeric(Me.MyNumericTextBox.Text) Then MyErrorProvider.SetError(Me.MyNumericTextBox, "") 'Fire or cancel the 'error in whatever way suites you, this simply clears the errorprovider Else MyErrorProvider.SetError(Me.MyNumericTextBox, "The value must be numeric") 'Fire or 'cancel the error in whatever way suites you, this sets the error on the errorprovider Me.MyNumericTextBox.Focus() 'This forces the user to enter the validated 'data before leaving the control End If End SubPlease reply to this post for more information.

                _______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit

                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