Numerical textbox validation, how to handle an empty string? [modified]
-
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
-
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
-
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
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
-
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
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
-
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
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?
-
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?
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
-
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
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 Sub
Please reply to this post for more information._______________________________________________________________________ http://www.readytogiveup.com/[^] "you can't forget something you never knew..." M. Du Toit