Hi All.
I validate TextBox for numeric and legth no more then 7 characters. I'm checking for Not IsNumber and Len. But I want to give a user to save record when TextBox is blank. If is it possible how to do it?
Thanks.
Printable View
Hi All.
I validate TextBox for numeric and legth no more then 7 characters. I'm checking for Not IsNumber and Len. But I want to give a user to save record when TextBox is blank. If is it possible how to do it?
Thanks.
Hey,
I am not sure that I fully understand the question?!?
Can you provide more details, plus the code you are already using?
Gary
Code:Dim BogusDec As Decimal
If (TextBox.Text.Trim.Length = 0I OrElse TextBox.Text.Trim.Length = 7I) AndAlso Decimal.TryParse(TextBox.Text.Trim, BogusDec) Then
'Good to go
End If
Hey,
If that is what the OP is trying to do, then I would be tempted to implement this as a Regular Expression check:
http://www.java2s.com/Tutorial/CShar...oraTextBox.htm
Hope that helps!!
Gary
Not Sure but do you have something like this in vb.net?
vb Code:
Dim BogusDec As Decimal If TextBox.Text.Trim.Length < 8I AndAlso Decimal.TryParse(TextBox.Text.Trim, BogusDec) Then 'Good to go End If
Regex is kind of over kill for this simple validation.
Set the textbox.MaxLength = 7 and handle the textbox.validating event. In the event handler, just test if the text is not blank and do a tryparse to make sure it's a valid number.
How about creating an Extension method?
http://dotnetbyexample.blogspot.com/...n-methods.html
Gary
Its easy to create extension methods.
e.g.
vb.net Code:
Public Module MyExtensionMethods <Extension()> _ Public Function IsNumber(ByVal input As String) As Boolean Return Decimal.TryParse(input, Nothing) End Function End Module
So now you use it just like any other method of the String class:
e.g.
vb.net Code:
Dim myString as String = "12345" MessageBox.Show(myString.IsNumber)
I guess it would be nice, but you can just use the IsNumeric function insteadQuote:
Didn't know that one, I thought it had to be an actual variable whether you use it or not. It would be nice if the String class had a .IsNumber function like the Char class does.
The IsNumeric function is good for ordinary use. But it usually won't give the results you would expect. It actually tries anything that can be possible as a number. e.g. hex numbers, commas etc. which you might not expect in normal ways.
e.g.
vb.net Code:
Decimal.TryParse("&H10A", Nothing) '<-- Returns False IsNumeric("&H10A") '<-- Returns True
Oh right, I never knew that, thanks :)