parseInt() returning incorrect value; DOB validation
<p>Wondering if anyone could shed some light as I’m pulling my hair out over this one!</p>

<p>I have written a simple function to validate a users Date of birth which is all well and good, or at least it was until I realised it wasn't working as expected!  </p>

<p>The function, as below, takes 2 parameters, dobNum (the value of an input field) and dmy (a switch variable that receives either ‘dd’, ‘mm’ or ‘yyyy’). The function is called as follows with the value of an input field so there shouldn’t be any object based problems:</p>

<p>onblur=”validateDOB(this.value, ‘mm’);</p>

<p>I have spent ages trying to get to the bottom of this and there seems to be a problem with the parseInt() statement.
This works fine for the days and months until you pass either a 08 (zero, eight) or a 09 (zero,nine).  Here the result of the parseInt() returns as 0 rather than an 8 or 9 respectively.<br>
But this is only a problem with 08 and 09, passing numbers 01 to 07 returns 1 to 7 as expected.<br>
Similarly, when passing single digits, 1 to 9, to the function, parseInt() returns the appropriate value as an integer.  </p>

<p>Really struggling to fathom this one out.  Conversely removing the parseInt statement completely seems to work however this leaves the dobNum value as a string which I don’t feel is particularly good practice.</p>

<p>Can anyone shed some light on this please? (this problem occurs in both firefox and IE)
Many thanks,
SMc</p>

<pre><code>var DOBddOK = false;
var DOBmmOK = false;
var DOByyyyOK = false;

function validateDOB (dobNum, dmy) {
  // Set Regexp based on dmy var.
    if (dmy.length == 2) var reg = /^([0-9]{1,2})$/;
      else  var reg = /^([0-9]{4})$/;
    var numOK = reg.test(dobNum);
    alert("NumOK: "+numOK); //test

  // If dobNum value passes regExp test then convert  it to an integer
    if (numOK) {
      var numVar = parseInt(dobNum); 
      //var numVar = dobNum;
      alert("NumVar: "+numVar); //test
    }
    alert("dmy: "+dmy); //test

    switch (dmy) {
      case "dd":
        if (numOK &amp;&amp; numVar &lt;= 31 &amp;&amp; numVar &gt; 0) DOBddOK = true;
        else DOBddOK = false;
        break;
      case "mm":
        if (numOK &amp;&amp; numVar &lt;= 12 &amp;&amp; numVar &gt; 0) DOBmmOK = true;
        else DOBmmOK = false;
        break;
      case "yyyy":
        var d = new Date();
        d = d.getFullYear();
        if (numOK &amp;&amp; numVar &lt;= (d-18) &amp;&amp; numVar &gt;= (d-80)) DOByyyyOK = true;
        else DOByyyyOK = false;
        break;
    }
}
</code></pre>
