Good day!

I have a variable string that will hold numbers...I need a function that will check if the number meets the below criteria:

( Acceptable format )
-accept positive numbers
-accept negative numbers
-accept single decimal place (ex. 567.89)
-will not accept double demical place (ex. 567.89.678)
-will not accept single, double positive sign after a digit(ex. 98+45+)
-will not accept single, double negative sign after a digit(ex.34-, 98-45-)

<A little code demonstration>

Public Function Validate_Number(ByRef PassNumber as String) as Boolean
 'formatting conditions here
End Function

Private Sub TxtInputNumber_LostFocus()
If Validate_Number(TxtInputNumber.Text)=True Then
   msgbox "The Number is in Acceptable Format",vbInformation
else
   msgbox "The Number is Not an Acceptable Format",vbInformation
End Sub

I dont want to use masking as this is the required format to follow.

Thank you for helping!

Recommended Answers

All 2 Replies

If you google the following you should get plenty of sample code. You will be making use of the following functions to check for an integer value, search for characters etc -

IsNumeric
cInt
Mid
Left
Right
Replace
ASCii

With the above you can perform any validation check you need.

Here's mine for your number validation

Function Validate_Number(ByRef PassNumber As String) As Boolean
    Static szValidNum As String
    If szValidNum = "" Then _
        szValidNum = "0123456789"

    Dim szNum As String
    Dim c As String * 1, i As Integer, ch As Integer
    Dim dots As Integer, pluses As Integer, minuses As Integer
    ' Create a local copy of the PassNumber
    szNum = PassNumber
    ch = Len(szNum)
    ' Trim trailing dots.
    ' A number of multiple dots is number if these dots are the end of the number.
    ' We must be considerate
    Do While 1
        If Right(szNum, 1) <> "." Then Exit Do
        ch = ch - 1
        szNum = Left(szNum, ch)
    Loop
    For i = 1 To ch
        c = Mid(szNum, i, 1)
        If 0 = InStr(1, szValidNum, c) Then
            Select Case c
                Case "+"
                    'Make sure positive sign exists before anything else
                    If i > 1 Then Exit Function
                    'Make sure we are the only sign in this number
                    If minuses Then Exit Function
                    pluses = pluses + 1
                    'Reject multiple pluses
                    If pluses = 2 Then Exit Function
                Case "-"
                    'Make sure negative sign exists before anything else
                    If i > 1 Then Exit Function
                    'Make sure we are the only sign in this number
                    If pluses Then Exit Function
                    minuses = minuses + 1
                    'Reject multiple pluses
                    If minuses = 2 Then Exit Function
                Case "."
                    dots = dots + 1
                    'Make sure we are the only dot in this decimal number
                    If dots = 2 Then Exit Function
                Case Else
                    'Hell we are not a number!!!
                    Exit Function
            End Select
        End If
    Next i
    ValidateNum = True
End Function
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.