Solved

numeric validation

Posted on 2011-02-28
7
271 Views
Last Modified: 2012-05-11
I have a textbox that receives inches as input.  I only want to allow the user to enter a whole number or a number with a .5 to denote a half inch.  Does anybody have an example of how to validate this?
0
Comment
Question by:oak29
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility

Dim value As String = Me.textBox1.Text
Dim converted As Decimal

If Decimal.TryParse(value, converted) AndAlso (converted - Int(converted) = 0.5) Then
    MsgBox("Yay!")
End If

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Correction:
Dim value As String = Me.textBox1.Text
Dim converted As Decimal

If Decimal.TryParse(value, converted) AndAlso ((converted - Int(converted) = 0.5) OrElse (converted - Int(converted) = 0.0)) Then
    MsgBox("Yay!")
End If

Open in new window

0
 
LVL 13

Expert Comment

by:gamarrojgq
Comment Utility
Hi,

You can do a couple of things, first use a NUMERICAUPDOWN control instead of a textbox, second set the DECIMALSPLACE property to 1, and finally you can put this code in the Validating Event



Dim intPart As Int32
        intPart = Int32.Parse(NumericUpDown1.Value.ToString.Substring(0, NumericUpDown1.Value.ToString.IndexOf(".")))
        If NumericUpDown1.Value > intPart And NumericUpDown1.Value <> (intPart + 0.5) Then
            MsgBox("Only Whole Numbers or Half", MsgBoxStyle.Critical)
            e.Cancel = True
        End If

Open in new window

0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 8

Expert Comment

by:PagodNaUtak
Comment Utility
Try the below code...
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress  
        Dim KeyAscii As Short = Asc(e.KeyChar)  
        ' Check if the typed character is a letter.  
        If Char.IsDigit(e.KeyChar) = False And _  
         Char.IsControl(e.KeyChar) = False Then  
            e.Handled = True  
        End If  
        ' Check if the typed character is a period.  
        If KeyAscii = 46 Then  
            e.Handled = False  
        End If  
    End Sub

Open in new window

0
 
LVL 5

Expert Comment

by:andr_gin
Comment Utility
1.) Using code like this can be dangerous as other countries use different formatting for numeric values. For example the German standard is a comma instead of a point. If you parse (4711.815) you get 47110815, because the dot is interpreted as thousand delimiter => not good for a finance application ;-)

2.) With this code you cant the sure, if the user enters two dots. There are some conditions you cannot check with the KeyPress. For example the Text "4711." is not valid, but the text "4711.5" is valid. So typing the dot will create a temporary invalid number, even if you do it the right way.

3.) You can use a masked textbox for helping the user to enter the correct format, but you have to check the data, when parsing it.

4.) Be sure to not check for =, when using decimal values. Because of the limited amount of binary digits a value like 0.1 will be stored as 0.1000000000000001 or 0.099999999999. For 0.5 values you do not have this problem directly, but after the first calculation.

5.) I would use code like this for the check method. Call this code when parsing the TextBox (Or MaskedTextBox). If you want, you can also call it in the Validating event after the user leaves the TextBox

Dim DoubleValue As DoubleValue
If Double.TryParse(TXT_YourTextBox.Text,DoubleValue Then
     Dim DoubleDoubleValue =DoubleValue*2
    If Math.Abs(DoubleDoubleValue - Math.Round(DoubleDoubleValue,0)) < 0.000001 Then
           MsgBox("Your Value is: " & DoubleValue)
    Else
        MsgBox("Only enter x.0 or x.5")
   EndIf
Else
MsgBox("Invalid Format")
EndIf
0
 
LVL 8

Expert Comment

by:PagodNaUtak
Comment Utility
@andr qin,

Nice, tips...

I am just a little curios why need this line of code?
Dim DoubleDoubleValue =DoubleValue*2
0
 
LVL 5

Expert Comment

by:andr_gin
Comment Utility
Well you want the user to enter only full or halfs, so I double it. Then it must be in integer value. So I compare the rounded value with the original value and if the difference is less then 10^-6 I accept it.

If it is not exactly 0.5 you can also write it this way:

Private Const LittleDouble as Double=0.000001

Dim DoubleValue As DoubleValue
If Double.TryParse(TXT_YourTextBox.Text,DoubleValue Then
     Dim Difference = Math.Abs(DoubleValue - Math.Round(DoubleValue,0))
    If Differnce < LittleDouble OrElse (Difference >= 0.5-LittleDouble  AndAlso Difference <= 0.5+LittleDouble) Then
           MsgBox("Your Value is: " & DoubleValue)
    Else
        MsgBox("Only enter x.0 or x.5")
   EndIf
Else
MsgBox("Invalid Format")
EndIf
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now