Javascript parseInt( ) Function Issue

This section contains a lot of Scripting material for your website.

Javascript parseInt( ) Function Issue

Postby Shahzad Butt on July 21st, 2008, 8:41 am

I have noticed in some applications specially in implementation of validation of credit card expiry date using javascript, that when we use the parseInt() function to convert the string values into integer values from a dropdown, the parseInt() funciton failed on the values "08" and "09" and returns 0 as integer. However if works fine for all other values like "01", "02", etc.

So i will recommend you people to use eval() instead of parseInt() function for converion of values from strings to integers.

You can view this issues from the following simple html file.

demo.html
Code: Select all
<html>

<head>
   <title>Javascript ParseInt( ) Issue</title>

   <script type="text/javascript">
   <!--

      function showValue(sValue)
      {
         alert("parseInt() = " + parseInt(sValue) + "  \n\n  " + "eval() = " + eval(sValue));
      }

   -->
   </script>

</head>

<body>

   <h1>Javascript ParseInt( ) Issue</h1>

   Please select the value "08" or "09" to see the issue.<br />
   <select onchange="showValue(this.value);">
      <option value="">- select -</option>
      <option value="01">01</option>
      <option value="02">02</option>
      <option value="03">03</option>
      <option value="04">04</option>
      <option value="05">05</option>
      <option value="06">06</option>
      <option value="07">07</option>
      <option value="08">08</option>
      <option value="09">09</option>
      <option value="10">10</option>
      <option value="11">11</option>
      <option value="12">12</option>
   </select>

</body>
</html>
Regards
Shahzad Butt
http://www.GeoSourceCode.com
Shahzad Butt
 
Posts: 19
Joined: July 9th, 2008, 11:16 am
Location: Pakistan

Return to Java Script / VB Script

Who is online

Users browsing this forum: No registered users and 0 guests

cron