{"id":64903,"date":"2007-05-09T22:08:00","date_gmt":"2007-05-09T22:08:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/05\/09\/how-can-i-prevent-users-from-typing-anything-but-numbers-into-an-hta-text-box\/"},"modified":"2007-05-09T22:08:00","modified_gmt":"2007-05-09T22:08:00","slug":"how-can-i-prevent-users-from-typing-anything-but-numbers-into-an-hta-text-box","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-prevent-users-from-typing-anything-but-numbers-into-an-hta-text-box\/","title":{"rendered":"How Can I Prevent Users From Typing Anything But Numbers Into an HTA Text Box?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! We have an HTA we use for data input. In one of the text boxes our users are only supposed to type numbers. How can I prevent users from typing anything except numbers in that text box?<BR><BR>&#8212; KV<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, KV. You know that\u2019s a pretty good question: is there a way to prevent users from typing anything except numbers into a text box? And the answer to your question is this: yes, yes there <I>is<\/I> a way to prevent users from typing anything except numbers into a text box. Thanks for the question, KV; we\u2019ll see everyone tomorrow!<\/P>\n<P>Hold on a second here; the Scripting Editor seems to have a problem with today\u2019s column. (God forbid we dangled a participle or something!) What\u2019s that? No, no, if KV wanted to know <I>how<\/I> to prevent users from typing anything except numbers into a text box then she would have <I>asked<\/I> how to do that. <\/P>\n<P>Oh, she did not; go back and read the question again and see for yourself.<\/P>\n<P>Oh. Well, she must have added that later, because we\u2019re pretty sure that wasn\u2019t in her original question.<\/P>\n<P>At any rate, we\u2019re back. As we were about to say (before we were so rudely interrupted), KV wants to know <I>how<\/I> to prevent users from typing anything except numbers into a text box. (Obviously.) How can she do that? To tell you the truth, we have no idea; heck, if we knew how to do things like that we\u2019d have <I>real<\/I> jobs.<\/P>\n<TABLE class=\"dataTable\" id=\"ERD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. In case you\u2019ve been thinking, \u201cI bet Microsoft only allows the best and the brightest of their employees to write the <I>Hey, Scripting Guy!<\/I> column,\u201d well, let\u2019s just say that you might want to think again.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>The truth is, KV, we don\u2019t <I>know<\/I> how to prevent users from typing anything except numbers into a text box. But let\u2019s try a few things and see what happens. Nope \u2026 Nope \u2026 Close, but not quite \u2026 Ouch! that hurt \u2026 No \u2026 Ah, there we go: we did it!<\/P>\n<P>Oops. False alarm; apparently W <I>isn\u2019t<\/I> a number. Back to the old drawing board \u2026. <\/P>\n<P>Hold the phone, KV; we just might have something for you after all:<\/P><PRE class=\"codeSample\">&lt;SCRIPT Language =&#8221;VBScript&#8221;&gt;\n    Sub KeyCheck\n        If Window.Event.KeyCode &lt; 48 OR Window.Event.KeyCode &gt; 57 Then \n            Window.Event.returnValue = False    \n        End If\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;BODY&gt;\n    &lt;INPUT TYPE=text onKeyPress=&#8221;KeyCheck&#8221;&gt;\n&lt;\/BODY&gt;\n<\/PRE>\n<P>It took a little poking around through the <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/ms533050(vs.85).aspx\" target=\"_blank\"><B>HTML and DHTML Reference<\/B><\/A> on MSDN, but this looks like this will do the trick. As you can see, we have a very simple little HTA here consisting of a single text box and a single subroutine. (What\u2019s that? You don\u2019t know what HTAs even <I>are<\/I>? Then you should take a look at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Zone<\/B><\/A>.) Let\u2019s start off by eyeballing the code that creates the text box:<\/P><PRE class=\"codeSample\">&lt;INPUT TYPE=text onKeyPress=&#8221;KeyCheck&#8221;&gt;\n<\/PRE>\n<P>Nothing too fancy there: we simply use the <B>INPUT<\/B> tag to create the text box, adding the <B>TYPE=text<\/B> parameter to ensure that we get a text box. (If you\u2019re new to HTML, the INPUT tag can also be used to add controls like buttons, checkboxes, and radio buttons to an HTA.) We then tack this parameter on to the end of the INPUT tag:<\/P><PRE class=\"codeSample\">onKeyPress=&#8221;KeyCheck&#8221;\n<\/PRE>\n<P>What does this do? Well, any time a user presses a key while working in an HTA (or on a Web page, for that matter), the <B>onKeyPress<\/B> event is fired. This parameter simply says, \u201cOK, HTA; here\u2019s the deal. If the onKeyPress event gets fired <I>and<\/I> if the focus happens to be in our text box, then run the subroutine named KeyCheck.\u201d In other words, if you\u2019re typing something in the text box, each time you press a key the HTA will call the KeyCheck subroutine.<\/P>\n<P>So then what does the KeyCheck subroutine do? Well, as you probably know, each key on the keyboard has a value assigned to it. (You can also find a list of keys \u2013 and their <A href=\"http:\/\/msdn2.microsoft.com\/en-us\/library\/6hy0yb50.aspx\" target=\"_blank\"><B>corresponding values<\/B><\/A> \u2013 on MSDN.) The first thing we do in this subroutine is check to see if the <B>KeyCode<\/B> (which happens to be a property of the onKeyPress event) is less than 48 or greater than 57:<\/P><PRE class=\"codeSample\">If Window.Event.KeyCode &lt; 48 OR Window.Event.KeyCode &gt; 57 Then\n<\/PRE>\n<P>Why would we do a crazy thing like that? To answer that, take a look at the values assigned to keyboard keys 0 through 9:<\/P>\n<TABLE class=\"dataTable\" id=\"E2F\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Key<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">0<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">48<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">1<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">49<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">50<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">51<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">52<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">53<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">6<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">54<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">7<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">55<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">8<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">56<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">9<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">57<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>In other words, if a key has a value of 48 through 57, then it has to be one of the numbers 0 through 9. By extension, that means that any key with a value less than 48, or any key with a value greater than 57, <I>can\u2019t<\/I> be one of the numbers 0 through 9. By checking the value of the KeyCode property we can determine which key the user typed.<\/P>\n<P>Now, if the user types a number between 0 and 9 that\u2019s fine; in that case we don\u2019t do anything except exit the subroutine. However, let\u2019s assume the user has pressed a key that <I>isn\u2019t<\/I> one of the numbers 0 through 9. For example, suppose the user types <I>x<\/I>, which has a value of 120. What happens then?<\/P>\n<P>This is what happens then:<\/P><PRE class=\"codeSample\">Window.Event.returnValue = False\n<\/PRE>\n<P>Interestingly enough, events have all sorts of properties associated with them, including <B>returnValue<\/B>. What does the returnValue property do? Well, when set to True (the default value) it returns the value of the event; in this case, that means it will return the fact that the user pressed a key on the keyboard. However, if returnValue is set to False then the event value is <I>not<\/I> returned; in a sense, it\u2019s as though the event never occurred. If a user presses any key other than one of the numbers 0 through 9, we set returnValue to False and the event value is discarded; that means that no character gets typed into the text box.<\/P>\n<P>Confused? Give it a try and see for yourself. If you type any of the numbers between 0 and 9 (inclusive) those values will appear in the text box. But if you type anything other than those numbers \u2026.<\/P>\n<P>Anyway, we hope that helps, KV. And we apologize for the confusion at the beginning of today\u2019s column; we had no idea that we were expected to explain <I>how<\/I> to prevent users from typing anything except numbers into a text box. Fortunately, the Scripting Editor was on the ball, and thanks to her diligence we ended up doing a bunch of research and hard work that we hadn\u2019t planned on doing, and on what could end up being the only nice day we ever get here in Redmond. (As we sit here typing, it\u2019s 70 degrees and sunny.) But duty is far more important than a sunny day in May, isn\u2019t it Scripting Editor?<\/P>\n<TABLE class=\"dataTable\" id=\"E2AAC\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. Can we tell that it\u2019s 70 degrees and sunny just by looking out our window? You bet we can. Or at least we could, if we actually <I>had<\/I> a window. In case you\u2019re wondering, the Scripting Guy who writes this column (despite having single-handedly <I>invented<\/I> the Script Center) doesn\u2019t have a window office. Interestingly, the Scripting Editor, whose claim to fame is that she \u2026 did \u2026 something \u2026 at some point in her life \u2026 <I>does<\/I> have a window office. But, then again, she <I>deserves<\/I> a window office.<\/P>\n<P>Come to think of it, she deserves a <I>lot<\/I> of things. Wouldn\u2019t you agree, Scripting Editor?<\/P>\n<P><I>Ed<\/I><I>itor\u2019s Reply: Absolutely. Probably not anything the Scripting Guy<\/I><I> who writes this column<\/I><I> is thinking of, but yes, the Scripting <\/I><I>Ed<\/I><I>itor deserves a lot of things<\/I><I> \u2013 <\/I>especially<I> her window office.<\/I><\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! We have an HTA we use for data input. In one of the text boxes our users are only supposed to type numbers. How can I prevent users from typing anything except numbers in that text box?&#8212; KV Hey, KV. You know that\u2019s a pretty good question: is there a way to [&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":[3,4,5,30],"class_list":["post-64903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! We have an HTA we use for data input. In one of the text boxes our users are only supposed to type numbers. How can I prevent users from typing anything except numbers in that text box?&#8212; KV Hey, KV. You know that\u2019s a pretty good question: is there a way to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64903","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=64903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64903\/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=64903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}