{"id":66433,"date":"2006-09-20T16:22:00","date_gmt":"2006-09-20T16:22:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/09\/20\/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\/"},"modified":"2006-09-20T16:22:00","modified_gmt":"2006-09-20T16:22:00","slug":"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","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/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\/","title":{"rendered":"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?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>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?<BR><BR>&#8212; MB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, MB. You want to talk irony?<\/P>\n<P>Oh. Well, we\u2019re going to talk irony anyway. In <A href=\"http:\/\/null\/technet\/scriptcenter\/resources\/qanda\/sept06\/hey0919.mspx\"><B>yesterday\u2019s column<\/B><\/A> 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 <I>use<\/I> that $50 million, or at least part of it.<\/P>\n<P>That\u2019s 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. \u201cTut, tut,\u201d he said. \u201cWhat\u2019s mere money when it comes to a child\u2019s happiness?\u201d <\/P>\n<P>It was only after they left the building that he asked the Scripting Son to please call 911, and right away.<\/P>\n<TABLE id=\"EID\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. 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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So what does all that mean? Well, for one thing, it means the Scripting Dad can\u2019t really afford to get fired in the next year. Which, in turn, means he should give you an answer to your question:<\/P><PRE class=\"codeSample\">Set objRegEx = CreateObject(&#8220;VBScript.RegExp&#8221;)<\/p>\n<p>objRegEx.Global = True   \nobjRegEx.Pattern = &#8220;[^A-Z0-9]&#8221;<\/p>\n<p>strSearchString = &#8220;Ken Myer&#8221;<\/p>\n<p>Set colMatches = objRegEx.Execute(strSearchString)  <\/p>\n<p>If colMatches.Count &gt; 0 Then\n    Wscript.Echo &#8220;The following characters are not allowed:&#8221;\n    For Each strMatch in colMatches   \n        Wscript.Echo  strMatch.Value\n    Next\nEnd If\n<\/PRE>\n<P>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\u2019ll use this simple little script to explain how the code works, then, at the end, we\u2019ll show you an example of how this might be embedded in an HTA.<\/P>\n<P>The script itself starts out by creating an instance of the <B>VBScript.RegExp<\/B> object; this is the object that allows us to use regular expressions in VBScript. (<B>Note<\/B>: You say y don\u2019t know what regular expressions are? Then take a look at this on-demand Scripting Guys <A href=\"http:\/\/null\/events\/EventDetails.aspx?CMTYSvcSource=MSCOMMedia&amp;Params=%7eCMTYDataSvcParams%5e%7earg+Name%3d%22ID%22+Value%3d%221032271679%22%2f%5e%7earg+Name%3d%22ProviderID%22+Value%3d%22A6B43178-497C-4225-BA42-DF595171F04C%22%2f%5e%7earg+Name%3d%22lang%22+Value%3d%22en%22%2f%5e%7earg+Name%3d%22cr%22+Value%3d%22US%22%2f%5e%7esParams%5e%7e%2fsParams%5e%7e%2fCMTYDataSvcParams%5e\" target=\"_blank\"><B>webcast<\/B><\/A>.) We then configure the following two properties of our regular expressions object:<\/P><PRE class=\"codeSample\">objRegEx.Global = True   \nobjRegEx.Pattern = &#8220;[^A-Z0-9]&#8221;\n<\/PRE>\n<P>Setting the <B>Global<\/B> property to True tells the script to match all occurrences of our search pattern. To be honest, we don\u2019t really need to do that here. After all, it probably suffices just to know that <I>one<\/I> invalid character appears in the search string; you probably don\u2019t need to know about <I>all<\/I> 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.)<\/P>\n<P>Second, we specify the pattern we are searching for. There are actually three pieces to our pattern:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The square brackets enclosing the search string. That means we\u2019re searching for a set of characters as opposed to a single character.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The ^ character. This is, officially, the \u201cnegation\u201d character. That just means that we\u2019re searching for anything that <I>isn\u2019t<\/I> part of our character set.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>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.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Ah, good question: why <I>did<\/I> we jam A-Z and 0-9 together; why didn\u2019t we separate them with a blank space, like so:<\/P><PRE class=\"codeSample\">[^A-Z 0-9]\n<\/PRE>\n<P>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\u2019t an uppercase A-Z, a number, <I>or<\/I> a blank space. Because blank spaces weren\u2019t part of the original criteria, we left them out of the character set.<\/P>\n<P>After defining our search string (the string value <I>Ken Myer<\/I>) we then call the <B>Execute<\/B> method and kick off the regular expression search:<\/P><PRE class=\"codeSample\">Set colMatches = objRegEx.Execute(strSearchString)\n<\/PRE>\n<P>After the search is complete, any characters that match the search pattern (again, that\u2019s any character that isn\u2019t an uppercase letter or a number) will be placed in the <B>Matches<\/B> collection. We can determine whether or not the string contains any invalid characters simply by checking the value of the collection\u2019s <B>Count<\/B> property; if the Count is greater than 0 that means a match (and, by extension, an invalid character) was found. Here\u2019s the code that checks the value of the Count property:<\/P><PRE class=\"codeSample\">If colMatches.Count &gt; 0 Then\n<\/PRE>\n<P>If the Count <I>is<\/I> greater then 0 we echo back a message reporting that invalid characters were found; we also echo back <I>which<\/I> invalid characters were found. That\u2019s what these lines of code do:<\/P><PRE class=\"codeSample\">Wscript.Echo &#8220;The following characters are not allowed:&#8221;\nFor Each strMatch in colMatches   \n    Wscript.Echo  strMatch.Value\nNext\n<\/PRE>\n<P>As you can see, we simply loop through all the items in the Matches collection, echoing back the <B>Value<\/B> of each item. In the case of the string <I>Ken Myer<\/I>, that results in output like this:<\/P><PRE class=\"codeSample\">The following characters are not allowed:\ne\nn<\/p>\n<p>y\ne\nr\n<\/PRE>\n<P>Now, suppose we change our string value to this: <I>KENMYER<\/I>. Take a look at the output we get in that case:<\/P><PRE class=\"codeSample\"><\/PRE>\n<P>That\u2019s right, no output at all. That\u2019s because our new string doesn\u2019t have any invalid characters.<\/P>\n<P>As for wrapping this all into an HTA, here\u2019s 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:<\/P><PRE class=\"codeSample\">&lt;Script language=&#8221;VBScript&#8221;&gt;\n        Sub VerifyInput\n        Set objRegEx = CreateObject(&#8220;VBScript.RegExp&#8221;)<\/p>\n<p>        objRegEx.Global = True   \n        objRegEx.Pattern = &#8220;[^A-Z0-9]&#8221;<\/p>\n<p>        strSearchString = BasicTextBox.Value<\/p>\n<p>        Set colMatches = objRegEx.Execute(strSearchString)  <\/p>\n<p>        If colMatches.Count &gt; 0 Then\n            strMessage = &#8220;The following characters are not allowed:&#8221; &amp; vbCrLf\n            For Each strMatch in colMatches   \n                strMessage = strMessage &amp;  strMatch.Value &amp; vbCrLf\n            Next\n            Msgbox strMessage\n        End If\n    End Sub\n&lt;\/Script&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;input type=&#8221;text&#8221; name=&#8221;BasicTextBox&#8221; size=&#8221;50&#8243;&gt;&lt;p&gt;\n    &lt;input id=runbutton  type=&#8221;button&#8221; value=&#8221;Verify Input&#8221; onClick=&#8221;VerifyInput&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>Hopefully that\u2019s what you had in mind, MB. <\/P>\n<TABLE id=\"EVG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. If you don\u2019t know what HTAs are, let alone how to build them, take a look at our <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Zone<\/B>.<\/A><\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As for next year\u2019s 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. \u201cYou know, Dad, someday I\u2019m going to sign a $100 million contract with the Mariners,\u201d said the Scripting Son, who was 9 or 10 at the time, \u201cAnd when I do, I\u2019m going to give you $10,000.\u201d<\/P>\n<P>True. But hey, $10,000 is $10,000, right?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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?&#8212; MB Hey, MB. You want to talk irony? Oh. Well, we\u2019re going to talk irony anyway. In yesterday\u2019s column we tried to be cute, posing as the illegitimate children of a non-existent [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[174,3,4,5,30],"class_list":["post-66433","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-regular-expressions","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>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?&#8212; MB Hey, MB. You want to talk irony? Oh. Well, we\u2019re going to talk irony anyway. In yesterday\u2019s column we tried to be cute, posing as the illegitimate children of a non-existent [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=66433"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/66433\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=66433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=66433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=66433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}