Data Type Conversion and IsNaN() Function in JavaScript

Let's see the the following example that helps.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="DemoApplication.index" %>      
  2.       
  3. <!DOCTYPE html>      
  4. <html>      
  5. <head runat="server">      
  6.     <title>Second Class of JavaScript</title>      
  7.     <script type="text/javascript">      
  8.         function addNumbers() {      
  9.             var first = document.getElementById("txtFirst").value;      
  10.             if (first == "") {      
  11.                 alert('First Number is Required.');      
  12.                 return;      
  13.                 document.getElementById("txtFirst").focus(this);      
  14.             }      
  15.             first = parseFloat(first);      
  16.             if (isNaN(first)) {      
  17.                 alert('This field only accepts Numeric Value.');      
  18.                 document.getElementById("txtFirst").focus(this);      
  19.             }      
  20.             var second = document.getElementById("txtSecond").value;      
  21.             if (second == "") {      
  22.                 alert('Second Number is Required.');      
  23.                 return;      
  24.                 document.getElementById("txtSecond").focus(this);      
  25.             }      
  26.             second = parseFloat(second);      
  27.             if (isNaN(second)) {      
  28.                 alert('This field only accepts Numeric Value.');      
  29.                 document.getElementById("txtSecond").focus(this);      
  30.             }      
  31.             document.getElementById("txtResult").value = first + second;      
  32.         }      
  33.     </script>      
  34. </head>      
  35. <body>      
  36.     <form runat="server">      
  37.         <table style="border: 1px solid black; font-family:Calibri">      
  38.             <tr>      
  39.                 <td>Enter the First Number :       
  40.                 </td>      
  41.                 <td>      
  42.                     <asp:TextBox ID="txtFirst" runat="server"></asp:TextBox>      
  43.                 </td>      
  44.             </tr>      
  45.             <tr>      
  46.                 <td>Enter the Second Number :       
  47.                 </td>      
  48.                 <td>      
  49.                     <asp:TextBox ID="txtSecond" runat="server"></asp:TextBox>      
  50.                 </td>      
  51.             </tr>      
  52.             <tr>      
  53.                 <td>Result is :       
  54.                 </td>      
  55.                 <td>      
  56.                     <asp:TextBox ID="txtResult" runat="server" ReadOnly="true"></asp:TextBox>      
  57.                 </td>      
  58.             </tr>      
  59.             <tr>      
  60.                 <td></td>      
  61.                 <td>      
  62.                     <input type="button" id="btnAdd" runat="server" value="Add" onclick="addNumbers()" />      
  63.                 </td>      
  64.             </tr>      
  65.         </table>      
  66.     </form>      
  67.       
  68. </body>      
  69. </html>    
In the preceding example:
  • We have the Script section within the Head section.
  • We have inserted a table that contains two columns and four rows.
  • Three text boxes are included with a button to add two numbers.
We want that when the button is clicked, it adds two numbers that have been entered by the users into the textboxes. But if a user does not enter a value in either of the text boxes, we want JavaScript to show an alert, and also if a user enters a string instead of a number, the script should also show an alert. The preceding code implements that.
 
 
The following explains the code:
  1. Create two variables for storing the numbers entered by the user. We have the variables "first" and "second".
     
  2. Using an if block, we are checking if the text box is empty. If it is empty, then it throws an alert but if something is written in it, it does not do the "if" block.
     
  3. Now we must convert the value entered by the user into a float data type because by default, the text box considers everything entered in it to be a string. We have done that using the "parse.Float()" function that is part of JavaScript. We have converted it into a Float data type instead of an Integer Data type because if a user enters a decimal value, the script ignores the decimal places and that is wrong. So, Float is the correct Data type to be converted.
     
  4. The "return" statement returns control to the webform after showing one alert. Otherwise, it shows all the alerts at the same time.
     
  5. Now, the isNaN() (isNotANumber) function checks whether the user has entered a numeric value or not. It returns true if the value is not a number and returns false if the value is a number.
     
  6. "focus()" returns the focus to the control.
     
  7. Finally, We have added the two variables created in the first step and returned that calculated value in the third TextBox "txtResult".
Finally, to use the script, we need to pass the name of the JavaScript function in the "onclick" event of the button control.


Similar Articles