Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Client Scripting
Expertise: Beginner
Jul 8, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

reading input field numbers


JavaScript can be used to access values in web form input fields. This is possible because JavaScript treats input fields as objects with several properties. One of these properties is a string object called "value".

The question "How can I obtain input field numbers?" can be posed as a two-part question:

  1. How can I obtain input field values?
  2. How can I make sure input field values are treated as numbers?
Let's discuss the answers to each of these sub-questions.

How can I obtain input field values?

In order to arbitrarily access input field values, the JavaScript programmer must understand the complex object-property structure of web documents.

input field
Web documents are represented in JavaScript by the "document" object. This object has many properties, some of which may be objects corresponding to forms displayed in the document. Form objects, in turn, also have properties, some of which may be objects corresponding to input fields in the form. As you might expect, input fields also have properties. One of these properties is a string object called "value".

To illustrate this better, let's consider the following code:

<html> <head> <meta name="WT.qs_dlk" content="XJVgrRzzFqxoCiEBoqu9@AAAABU"/> <meta name="WT.qs_shmv" content="hv20180802-holmes.sf.quinstreet.net"/> <meta name="inject_params" content="WT.qs_dlk=XJVgrRzzFqxoCiEBoqu9@AAAABU&"/> <script>!function(){if(!window.BOOMR||!window.BOOMR.version){var e,t,n,o=document.createElement("iframe");o.src="javascript:false",o.title="",o.role="presentation",(o.frameElement||o).style.cssText="width:0;height:0;border:0;display:none;",(n=document.getElementsByTagName("script")[0]).parentNode.insertBefore(o,n);try{t=o.contentWindow.document}catch(n){e=document.domain,o.src="javascript:var d=document.open();d.domain='"+e+"';void(0);",t=o.contentWindow.document}t.open()._l=function(){var t=this.createElement("script");e&&(this.domain=e),t.id="boomr-if-as",t.src="/imageserver/common/sg/1.0/sg.min.js",BOOMR_lstart=(new Date).getTime(),this.body.appendChild(t)},t.write('<body onload="document._l();">'),t.close()}}();</script> <script> document.cookie = 'inject_params=WT.qs_dlk=XJVgrRzzFqxoCiEBoqu9@AAAABU&; path=/; domain=devx.com;'; document.cookie = 'WMUUID=XJVgrRzzFqxoCiEBoqu9@AAAABU; path=/; domain=devx.com;'; </script> <script> function increase () { var temp = parseInt (document . apple . banana . value); if (isNaN (temp)) return; document . apple . banana . value = temp * 2; } </script> <script> window.webtrendsAsyncInit=function(){ var dcs=new Webtrends.dcs().init({ dcsid:"dcsetaek200000gcgxfcx07g4_2p3u", domain:"www.qsstats.com", timezone:-8, i18n:true, onsitedoms:"devx.com", fpcdom:".devx.com", plugins:{ //hm:{src:"//s.webtrends.com/js/webtrends.hm.js"} } }).track(); }; (function(){ var s=document.createElement("script"); s.async=true; s.src="/imageserver/common/v10/webtrends.min.js"; var s2=document.getElementsByTagName("script")[0]; s2.parentNode.insertBefore(s,s2); }()); </script> <noscript><img alt="dcsimg" id="dcsimg" width="1" height="1" src="//www.qsstats.com/dcsetaek200000gcgxfcx07g4_2p3u/njs.gif?dcsuri=/index.php/tips/Tip/23740&amp;WT.js=No&amp;WT.tv=10.4.1&amp;dcssip=www.devx.com&amp;WT.qs_dlk=XJVgrRzzFqxoCiEBoqu9@AAAABU&"/></noscript> </head> <body> <form name=apple> Enter an integer and push the button to double it. <p> <input type=text name=banana> <input type=button value=&quot;increase&quot; onClick=&quot;increase ();&quot;> </form> </body> </html>
On your browser, this code produces a document with the following appearance:
Enter an integer and push the button to double it.

This JavaScript document allows the user to enter a value in the text field and and double it by pushing the button. When the text field does not contain a valid numeric string, pushing the button has no effect.

The value of the input field in this document can be accessed through the object called "document". Notice that in the document is a form, which has carefully been given the name, "apple". Because this form has a name, we can directly refer to the form object as a property of the document object, namely, "document.apple".

The text field in the form also has a name, "banana". Through this name, we can refer to the text field object as a property of the form object, namely, "document.apple.banana".

Finally, the text field has an object property called "value", which is a string object.

Thus, the value of the text field can be accessed through the object-property path "document.apple.banana.value". This expression will work in JavaScript code placed anywhere in the document. However, if the code is part of an event handler for the text field, then the shortcut-name "this.value", or even just "value", can also be used to mean the same thing.

How can I make sure input field values are treated as numbers?

I mentioned earlier that the value property of an input field object is a string. Thus, in order to treat value like a number, it must first be converted to an integer through the parseInt function.

The function parseInt, in its simplest form, accepts a string as input and returns the base-10 number represented by the string. Thus, in the sample code given above, the value of the input field is obtained through the expression

var temp = parseInt (document . apple . banana . value);
parseInt fails when the string does not contain digits that can be converted into a number. When this happens, it returns a special value called "NaN", which means "Not a Number." Thus, whenever parseInt is used to convert a string to a number, a special check should be made to determine whether the value returned is a legitimate number or a NaN.

The JavaScript language has a function called isNaN that serves this purpose. isNaN accepts a single parameter of any type and returns the boolean value true if the parameter is a NaN, false otherwise. In the above example, we check the value obtained from parseInt with the following statement:

if (isNaN (temp)) return;
This makes it possible for our code to avoid performing arithmetic on non-numeric strings.
DevX Pro
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date