make sure string only contains alphanumeric & DOT

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

make sure string only contains alphanumeric & DOT

Post by Robie »

Hi

Is there an easy way to check if a string contains only alphanumeric and 'dot' characters? For example, a valid string might contain "12.1.6z", "1.2.3", "3.3.9cc", etc. No other characters are allowed.

Thanks.
Robie

User avatar
HansV
Administrator
Posts: 78917
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: make sure string only contains alphanumeric & DOT

Post by HansV »

Do you want to allow letters with accents such as é or ü ?
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: make sure string only contains alphanumeric & DOT

Post by Robie »

HansV wrote:Do you want to allow letters with accents such as é or ü ?
Thanks for quick response Hans.
The answer is no - standard alphabet is fine.

User avatar
HansV
Administrator
Posts: 78917
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: make sure string only contains alphanumeric & DOT

Post by HansV »

You can use this function:

Code: Select all

Function TestString(s As String) As Boolean
    Dim re As Object
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "^[A-Za-z0-9.]+$"
    TestString = re.Test(s)
End Function
Example of use:

Code: Select all

    Dim MyString As String
    MyString = "Ro.b.ie.37"
    If TestString(MyString) Then
        ' MyString meets the condition
    Else
        ' MyString does not meet the condition
    End If
Best wishes,
Hans

Robie
5StarLounger
Posts: 656
Joined: 18 Feb 2010, 14:26

Re: make sure string only contains alphanumeric & DOT

Post by Robie »

HansV wrote:You can use this function:

Code: Select all

Function TestString(s As String) As Boolean
    Dim re As Object
    Set re = CreateObject("VBScript.RegExp")
    re.Pattern = "^[A-Za-z0-9.]+$"
    TestString = re.Test(s)
End Function
Example of use:

Code: Select all

    Dim MyString As String
    MyString = "Ro.b.ie.37"
    If TestString(MyString) Then
        ' MyString meets the condition
    Else
        ' MyString does not meet the condition
    End If
Thank you so much Hans, will try it out soon. :clapping: