Friday, September 21, 2012

How to check that user enters only digits

IsNumeric() function returns true for a string, which can me converted to number, like "1 2", "1e2". But if you need to check that string contains only digits? The simplest way I think is:

If strVar Like String(Len(strVar),"#") then
       'Only numbers!
End If

2 comments:

  1. What about

    Function IsDigitsOnly(strVar As String) As Boolean
    IsDigitsOnly = (strVar Like "[0-9]")
    End Function

    ReplyDelete
  2. Sorry, that's not correct.

    ReplyDelete