
September 25th, 2009, 04:16 AM
|
 |
Moderator
|
|
Join Date: Mar 2006
Location: South Wales
|
|
Quote: | Originally Posted by kmsen I want to get only integers (not other characters) from a text in a text box
Eg: If a text box says "I'm 20 years old" , I want to have 20 as my output
Thanks all, HELP ME | Hi, and welcome to the forums. You can use a regular expression to remove all non-numeric characters from your string. To use regex you need to add a reference to the "Microsoft VBScript Regular Expressions 5.5" library via Project - Add Reference.
Heres an example:
Code:
Private Sub Form_Load()
Dim myAge As String
myAge = getAge("I'm 20 years old")
MsgBox (myAge)
End Sub
Private Function getAge(ByVal mString As String) As String
Dim myRegExp As RegExp
Dim myMatches As MatchCollection
Dim myMatch As Match
Set myRegExp = New RegExp
myRegExp.IgnoreCase = True
myRegExp.Global = True
myRegExp.Pattern = "\D"
mString = myRegExp.Replace(mString, "")
getAge = mString
Set myRegExp = Nothing
End Function
An alternative to this would be to only allow the user to enter a valid number, either via a dropdown list of numbers which they can select from, or by adding some validation to the textbox to ensure that they only enter a number.
|