Kirszu
Kirszu

Reputation: 364

Return true if string contains a long (+10 digits) number

I would like to check if the provided string contains a number with more than ten digits.

exampleString = aaa123aaa12345aaaa   <--- not true
exampleString = 12345678   <--- not true
exampleString = 1234567,12345,123345   <--- not true
exampleString = aaaa12345678901234aaaa124aaa <-- true

I was trying to do something like extracting numbers to individual strings and then check if their length > 10, but I was unsuccesfull.

Any help or suggestions?

Upvotes: 0

Views: 599

Answers (3)

Shai Rado
Shai Rado

Reputation: 33692

Using RegEx object:

Option Explicit

Sub TestReg()

' sub to test RegEx10Digits function

Dim exampleString   As String

exampleString = "aaaa12345678901234aaaa124aaa"
MsgBox RegEx10Digits(exampleString)

End Sub

Function RegEx10Digits(RegString As String) As Boolean

' RegEx variables
Dim Reg As Object
Dim RegMatches As Variant

RegEx10Digits = False

Set Reg = CreateObject("VBScript.RegExp")
With Reg
    .Global = True
    .IgnoreCase = True
    .Pattern = "\d{11,}" ' Match any set of 10+  digits
End With

Set RegMatches = Reg.Execute(RegString)
If RegMatches.Count >= 1 Then ' make sure there is at least 1 match
    RegEx10Digits = True
End If

End Function

Upvotes: 0

Alex K.
Alex K.

Reputation: 175876

How about a simple Like?

if str like "*###########*" then ...

Upvotes: 3

JohnyL
JohnyL

Reputation: 7152

Function IsMore10Digits(strVal)
    With CreateObject("VBScript.RegExp")
        .Pattern = "\d{11,}": IsMore10Digits = .Test(strVal)
    End With
End Function

Upvotes: 1

Related Questions