How Can I Verify That All the Characters in a String are Either A-to-Z (Uppercase Letters) or the Digits 0-through-9 in a HTA?


Hey, Scripting Guy! Question

Hey, Scripting Guy! In an HTA, how can I verify that all the characters are either A-to-Z (uppercase letters) or the digits 0-through-9?

— MB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, MB. You want to talk irony?

Oh. Well, we’re going to talk irony anyway. In yesterday’s column we tried to be cute, posing as the illegitimate children of a non-existent defense minister and trying to talk people into helping us smuggle $50 million out of the make-believe country of Freedonia. Ironically, today we could actually use that $50 million, or at least part of it.

That’s because the Scripting Son has decided to play baseball for a local select team next season, a decision that will cost the Scripting Dad $1,800. To his credit, the Scripting Dad remained outwardly calm and stoic as the financial obligations were explained to him. “Tut, tut,” he said. “What’s mere money when it comes to a child’s happiness?”

It was only after they left the building that he asked the Scripting Son to please call 911, and right away.

Note. Which, of course, the Scripting Son declined to do. After all, once the check was signed he had no more use for the Scripting Dad anyway.

So what does all that mean? Well, for one thing, it means the Scripting Dad can’t really afford to get fired in the next year. Which, in turn, means he should give you an answer to your question:

Set objRegEx = CreateObject(“VBScript.RegExp”)

objRegEx.Global = True objRegEx.Pattern = “[^A-Z0-9]”

strSearchString = “Ken Myer”

Set colMatches = objRegEx.Execute(strSearchString)

If colMatches.Count > 0 Then Wscript.Echo “The following characters are not allowed:” For Each strMatch in colMatches Wscript.Echo strMatch.Value Next End If

What we just showed you is a simple VBScript script for determining whether anything other than an uppercase letter or a digit (0 through 9) appears in a string. We’ll use this simple little script to explain how the code works, then, at the end, we’ll show you an example of how this might be embedded in an HTA.

The script itself starts out by creating an instance of the VBScript.RegExp object; this is the object that allows us to use regular expressions in VBScript. (Note: You say y don’t know what regular expressions are? Then take a look at this on-demand Scripting Guys webcast.) We then configure the following two properties of our regular expressions object:

objRegEx.Global = True   
objRegEx.Pattern = “[^A-Z0-9]”

Setting the Global property to True tells the script to match all occurrences of our search pattern. To be honest, we don’t really need to do that here. After all, it probably suffices just to know that one invalid character appears in the search string; you probably don’t need to know about all the invalid characters. But, what the heck: consider this a Scripting Guys bonus. (And if you like that sort of service, well, we do accept tips. Or at least one of us does, as of today anyway.)

Second, we specify the pattern we are searching for. There are actually three pieces to our pattern:

The square brackets enclosing the search string. That means we’re searching for a set of characters as opposed to a single character.

The ^ character. This is, officially, the “negation” character. That just means that we’re searching for anything that isn’t part of our character set.

The character set A-Z0-9. As you can see, there are actually two components to this character set. Our two components are the characters A-Z (uppercase) and the numbers 0-9. If a character in the string is neither an uppercase letter nor a number it will trigger a match.

Ah, good question: why did we jam A-Z and 0-9 together; why didn’t we separate them with a blank space, like so:

[^A-Z 0-9]

Well, we could have. However, by doing so we would have defined a third component for our character set: a blank space. In that case, the script would match anything that wasn’t an uppercase A-Z, a number, or a blank space. Because blank spaces weren’t part of the original criteria, we left them out of the character set.

After defining our search string (the string value Ken Myer) we then call the Execute method and kick off the regular expression search:

Set colMatches = objRegEx.Execute(strSearchString)

After the search is complete, any characters that match the search pattern (again, that’s any character that isn’t an uppercase letter or a number) will be placed in the Matches collection. We can determine whether or not the string contains any invalid characters simply by checking the value of the collection’s Count property; if the Count is greater than 0 that means a match (and, by extension, an invalid character) was found. Here’s the code that checks the value of the Count property:

If colMatches.Count > 0 Then

If the Count is greater then 0 we echo back a message reporting that invalid characters were found; we also echo back which invalid characters were found. That’s what these lines of code do:

Wscript.Echo “The following characters are not allowed:”
For Each strMatch in colMatches   
    Wscript.Echo  strMatch.Value

As you can see, we simply loop through all the items in the Matches collection, echoing back the Value of each item. In the case of the string Ken Myer, that results in output like this:

The following characters are not allowed:

y e r

Now, suppose we change our string value to this: KENMYER. Take a look at the output we get in that case:

That’s right, no output at all. That’s because our new string doesn’t have any invalid characters.

As for wrapping this all into an HTA, here’s a very simple example, an HTA that contains a text box for entering data and a button that, when clicked, calls a subroutine that validates all the characters typed into that text box:

<Script language=”VBScript”>
        Sub VerifyInput
        Set objRegEx = CreateObject(“VBScript.RegExp”)

objRegEx.Global = True objRegEx.Pattern = “[^A-Z0-9]”

strSearchString = BasicTextBox.Value

Set colMatches = objRegEx.Execute(strSearchString)

If colMatches.Count > 0 Then strMessage = “The following characters are not allowed:” & vbCrLf For Each strMatch in colMatches strMessage = strMessage & strMatch.Value & vbCrLf Next Msgbox strMessage End If End Sub </Script>

<body> <input type=”text” name=”BasicTextBox” size=”50″><p> <input id=runbutton type=”button” value=”Verify Input” onClick=”VerifyInput”> </body>

Hopefully that’s what you had in mind, MB.

Note. If you don’t know what HTAs are, let alone how to build them, take a look at our HTA Developers Zone.

As for next year’s baseball season, well, the Scripting Dad has decided to view this as an investment in the future, After all, several years ago he and the Scripting Son were watching Sports Center about the same time that Edgar Martinez signed a new multi-year contract with the Seattle Mariners. “You know, Dad, someday I’m going to sign a $100 million contract with the Mariners,” said the Scripting Son, who was 9 or 10 at the time, “And when I do, I’m going to give you $10,000.”

True. But hey, $10,000 is $10,000, right?


Discussion is closed.

Feedback usabilla icon