Beginner Beginner - 4 months ago 14
Vb.net Question

How to check if a string contains only numbers?

Dim number As String = "07747(a)"

If number.... Then

endif


I want to be able to check inside the string to see if it only has number, if it does only contain numbers then run whatever is inside the if statment? What check do i use to check if the string only contains numeric and no alpha ot () etc ..?

What i am trying to check for is mobile numbers, so 077 234 211 should be accepted, but other alphas should not be

Answer

You could use a regular expression like this

If Regex.IsMatch(number, "^[0-9 ]+$") Then

...

End If