I'm trying to write a piece of code whereby the user who enters a numeric number in a field can only enter 2 numbers to the right of the decimal point and as many as they want to the left. Right now I can get it so it will only allow 2 numbers to the right but it doesn't allow me to put any numbers on the left of the decimal point unless you delete the decimal point first, does anyone know how to solve this one? Here's the code I have so far:
Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
'validate the entry for a textbox limiting it to only numeric values and the decimal point
'accept only one instance of the decimal point
If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True
If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
If InStr(oTextBox.Text, ".") = 0 Or Len(Mid(oTextBox.Text, InStr(oTextBox.Text, ".") + 1, 3)) < 2 Then
Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
Else
Return True
End If
End If
Return False 'for backspace
End Function
Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
'validate the entry for a textbox limiting it to only numeric values and the decimal point
'accept only one instance of the decimal point
If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True
If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
If InStr(oTextBox.Text, ".") = 0 Or Len(Mid(oTextBox.Text, InStr(oTextBox.Text, ".") + 1, 3)) < 2 Then
Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
Else
Return True
End If
End If
Return False 'for backspace
End Function