Thank you for your explanation. I realize there are sometimes things we are forced to do in the real world that appear as if they are homework assignments, thats why I prefer to ask.
The easiest way I could think of to extract the numbers would be a regular expression. I have another thought if your trying to compare two records but I'll get into that afterwards.
Basically with a regular expression you could specify something simple like "\d+" as the pattern, execute it against your string, and receive back a collection of all numbers in the string. Something like:
Code:
Dim objRegExp
Set objRegExp = New RegExp
objRegExp.Global = True
objRegExp.Pattern = "\d+"
Dim matches, match
Set matches = objRegExp.Execute(yourString)
For Each match in matches
Response.Write "Found: " & match.Value
Next
Set objRegExp = Nothing
Honestly I can't think of a better way to combine these than a series of loops. Maybe the coffee hasn't completely kicked in yet.
Now the other method I considered is only available if you are checking one input against one address field (ie, you have pulled a single field for the credit card number already). Basically you could do a character by character comparison through the first [string to verify] string looking for characters from the second [entered numeric string]. By looping through the second string looking for matching characters from the first you can ensure that all the numbers from the second string exist in the same order in the first string, even if there happen to be more letters and other numbers. Something like:
Code:
Dim stToVerify : stToVerify = "217 23rd St. Apt. 5"
Dim stOfficial : stOfficial = "217235"
Dim pos, vpos
vpos = 1
For pos = 1 To Len(stOfficial)
'advance through the entered string
Do While vpos < Len(stToVerify) + 1
'found a matching character, increment pos and return
If Mid(stToVerify,vpos,1) = Mid(stOfficial,pos,1) Then
vpos = vpos + 1
Exit Loop
End If
'didn't find match, advance to next character
vpos = vpos + 1
Loop
'if we have run out of characters to verify, jump out
If vpos > Len(stToVerify) + 1 Then Exit For
Loop
'Verification means the pos counter is past the end of the official string
If pos > Len(stOfficial) Then
Response.Write "Success: All characters from official were matched"
Else
Response.Write "Failed. At least one character could not be found."
Now that I have done that I thought of a way to combine these efforts. A single loop through the official string would allow us to build a regular expression based on it, at which point we would only have to make sure that the string to verify matches the pattern. Something like:
Code:
Dim stToVerify : stToVerify = "217 23rd St. Apt. 5"
Dim stOfficial : stOfficial = "217235"
Dim objRegExp, pattern, pos
'build a pattern based on the official set of numbers
pattern = ".*"
For pos = 1 to Len(stOfficial)
pattern = pattern & Mid(stOfficial,pos,1) & ".*"
Loop
'test the string to verify with the pattern
Set objRegExp = New RegExp
objRegExp.Global = True
objRegExp.Pattern = pattern
If objRegExp.Test(stToVerify) Then
Response.Write "Success: All the numbers in the official string appear in the same order in the verify string"
Else
Response.Write "Fail: something didn't match"
End If
In any case, I haven't tested any of the code I wrote above, as I wrote it directly into the post, but it should be close enough to give you the idea I was going for in each one. The second two are slightly less secure than the first method in that they can be broken if the end user is allowed to enter inordinately long strings for an address line (ie, 80-???? characters without spaces, letters, or special characters)
-T
Best MS KB Ever: