Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Floating Point Numbers Validation
[ All Languages » VB »  String]

Total Hit ( 1982)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Click here to copy the following block
Private Sub Form_Load()
  Text1.Text = Empty
End Sub

' USAGE
'Create a form a text box
Private Sub Text1_KeyPress(KeyAscii As Integer)
  ' Precision is 2 in this case
  ' Send the Precision Value same in both the Functions
  Call ValidateNum(Text1, 2, KeyAscii)
End Sub

Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
  Call ValidateDel(Text1, 2, KeyCode)
End Sub

Click here to copy the following block
Option Explicit
' For validating the Floating Point Numbers
' MODULE
Public Sub ValidateDel(txtnum As TextBox, ByVal m_Precision As Integer, KeyCode As Integer)

  Dim m_NumBeforeDot As Integer, m_NumAfterDot As Integer
  Dim TempInt As Integer

  If KeyCode = vbKeyDelete Then
    If m_Precision > 0 Then
      TempInt = InStr(txtnum, ".")
      If TempInt > 0 Then
        m_NumBeforeDot = TempInt - 1
        m_NumAfterDot = Len(txtnum) - TempInt
      Else
        m_NumBeforeDot = Len(Trim(txtnum))
        m_NumAfterDot = 0
        TempInt = txtnum.MaxLength - m_Precision
      End If
      
      If (txtnum.SelStart = TempInt - 1) Then
        If m_NumBeforeDot + m_NumAfterDot >= txtnum.MaxLength - m_Precision Then
          KeyCode = 0
        End If
      End If
    End If
  End If
End Sub

Public Sub ValidateNum(txtnum As TextBox, ByVal m_Precision As Integer, KeyAscii As Integer)
  
  Dim m_NumBeforeDot As Integer, m_NumAfterDot As Integer
  Dim TempInt As Integer

  If (KeyAscii <> vbKeyReturn) And _
    ((KeyAscii < vbKey0) Or (KeyAscii > vbKey9)) And _
    (KeyAscii <> vbKeyBack) Then
    
    If KeyAscii = 46 Then ' Dot.
      If m_Precision = 0 Then
        KeyAscii = 0
        Exit Sub
      Else
        If InStr(txtnum, ".") > 0 Then
          KeyAscii = 0
          
          Exit Sub
        End If
      End If
    Else
      KeyAscii = 0
      Exit Sub
    End If
    
  End If
  
  If KeyAscii = 0 Then Exit Sub


  If m_Precision > 0 Then
     TempInt = InStr(txtnum, ".")
     If TempInt > 0 Then
       m_NumBeforeDot = TempInt - 1
       m_NumAfterDot = Len(txtnum) - TempInt
     Else
       m_NumBeforeDot = Len(txtnum)
       If txtnum.SelStart < m_NumBeforeDot - m_Precision Then
         KeyAscii = 0
         Exit Sub
       End If
       m_NumAfterDot = 0
       TempInt = txtnum.MaxLength - m_Precision
     End If
    
     If KeyAscii >= vbKey0 And KeyAscii <= vbKey9 Then
       If Not (((m_NumBeforeDot < txtnum.MaxLength - m_Precision - 1) _
         And (txtnum.SelStart < TempInt)) Or _
         ((m_NumAfterDot < m_Precision) And (txtnum.SelStart >= TempInt))) Then
        
         KeyAscii = 0
        
       End If
     ElseIf (KeyAscii = vbKeyBack) And (txtnum.SelStart = TempInt) Then
         If m_NumBeforeDot + m_NumAfterDot >= txtnum.MaxLength - m_Precision Then
           KeyAscii = 0
         End If
     End If
   End If
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.