0

i wanted to know whats the error in this code. i wanted to compare today's date with the user input date

<script type="text/javascript">
function validate()
{
if( document.myForm.name.value == "" )
   {
     alert( "Please provide your date of birth!" );
     document.myForm.dob.focus() ;
     return false;
    }
var q = new Date();
var m = q.getMonth();
var d = q.getDate();
var Y = q.getFullYear();
var date = new Date(Y,m,d);
var r = document.getElementById(dates).value;
var m1 = r.getMonth();
var d1 = r.getDate();
var Y1 = r.getFullYear();
var mydate = new Date(Y1,m1,d1) ;
if(date<=mydate)
{
    alert("greater");
    return false;
}
else
{
    alert("smaller");
    return false;
}
return  true;
}
</script>
<form name="myForm" method="post" onsubmit= "return(validate());">
Name: <input type="text" name="name"><br>
Date: <input  type="date" id="dates" name="dates">
<br/>
<INPUT TYPE="submit"  VALUE="Submit" >
</form>

i felt like "var r = document.getElementbyid(dates).value;" this piece of code is not working

3 Answers3

1

The function name should be:

document.getElementById

(you are missing some capital letters)

Also, you are treating the result of document.getElementById(dates).value as though it will give you a Date object back, but it will actually give you are string - you need to convert this into a Date before you can call getMonth/getData/getFullYear on it - the answers to this question should help you here.

Community
  • 1
  • 1
codebox
  • 19,927
  • 9
  • 63
  • 81
1

I have put comments below where the main errors in the code are.

function validate()
{
    if( document.myForm.name.value == "" )  // Not an error per se, but you should use === to compare strings this way
    {
        alert( "Please provide your date of birth!" );
        document.myForm.dob.focus() ;
        return false;
    }
    var q = new Date();
    var m = q.getMonth();
    var d = q.getDate();
    var y = q.getFullYear();
    var date = new Date(Y,m,d);      // The variable Y is not defined. You probably meant lowercase y.
    var r = document.getElementbyid(dates).value; // Wrong case. Should be: document.getElementById
    var m1 = r.getMonth();
    var d1 = r.getDate();
    var y1 = r.getFullYear();
    var mydate = new Date(Y,m,d) ; // Same thing, probably meant lowercase y again.
    if(date<=mydate)
    {
        alert("greater");
        return false;
    }
    else
    {
        alert("smaller")   // This statement is missing a semicolon
        return false;
    }
    return  true;
}

Also, it's not entirely clear what you are trying to do. At the end, assuming you fix those syntactical errors, you have three Date variables:

  • q, which is created with the current date and time.

  • date, which is just passed the same current date and time that was in q.

  • mydate, which... also holds the current date and time, since you just passed it the same values from q that you passed to date.

So since the two date objects you are comparing are set to the same date, you will always alert "greater" (which is not really greater, because they are equal, but that is a separate issue).

I also find it strange that this validation function returns false in all cases. Because of your if/else statement, there is no way to ever return true. For a function that is supposed to validate something, it seems odd to have the validation always return false.

Additionally, as @codebox stated in his answer, document.getElementById(dates).value is just returning a string, which you must then convert into a new Date object if you wish to read the date values from it into your m1, d1, and y1 variables (which, as of right now, are never even used).

Calvin Scherle
  • 312
  • 1
  • 9
0

You are having couple of typo and logic mistake. This code should work.

 function validate()
 {
 if( document.myForm.name.value == "" )
    {
      alert( "Please provide your date of birth!" );
      document.myForm.dob.focus() ;
      return false;
     }
 var q = new Date();
 var m = q.getMonth();
 var d = q.getDate();
 var y = q.getFullYear();
 var date = new Date(y,m,d);
 var r = Date.parse(document.getElementById("dates").value);

 if(date<=r)
 {
     alert("greater");
     return false;
 }
 else
 {
     alert("smaller");
     return false;
 }
 return  true;
 }
Joey Chong
  • 1,470
  • 15
  • 20