Check TextField for certain characters?

Can someone please tell me what I need to look for in the Language reference, regarding checking a TextField to see if it does NOT contain the numbers 1, 2,3,4,5,6,7,8 or 9

Basically I need to check a TextField, and if it DOES contain any of the numbers 1-9, then do nothing, otherwise, do something.

The Language reference is so large, and I don’t even know what to look for :slight_smile:

Thank you.

You’d use a regular expression. The pattern is simply [1-9] so a match would mean it does contain one of those numbers.

dim rx as new RegEx
rx.SearchPattern = "[1-9]"
dim match as RegExMatch = rx.Search( theTextField.Text )
if match is nil then // Doesn't have a number
else // Does have a number
end if

Kem to the rescue once again !
I had no chance of stumbling across that solution :slight_smile:

Thank you.

Put this in a Module and make set the functions scope to “global”:

Function ContainsDigits(Extends str As String) As Boolean Dim mb As MemoryBlock = str Dim ub As Integer = str.Len - 1 For i As Integer = 0 To ub If mb.Byte(i) >= 48 And mb.Byte(i) <= 57 Then Return True End Next Return False End Function
Then use it like this:

If TextField1.Text.ContainsDigits Then ... End

Thanks Eli - I will save that snippet for future reference!

Thank you.

Just FYI, Eli’s code will look for any digit, including “0”, and will probably be slower than the RegEx.

There is also a bug. It should use the MemoryBlock.Size instead of the str.Len. Otherwise a multibyte character would throw off the count.