0

I just started learning php and from time to time I need some JS functions. Today is one of those day. I have an HTML form where I ask for the age of my users and I'd like to check if they put a grater or equal date compared to the today date. This is the HTML code:

var borndate = document.getElementById("borndate");
var date = new Date()
	 
function comparedate(){
	if (borndate >= date.getFullYear()){
			alert("Looks like you are to young!");
                        return false;
	} else {
		return true;
	}
 }
<div class="form">
    	<form class="login-form" method="POST" action="main.php">
    		<input type="date" id="borndate" name="borndate" required="required"/>
                <input type="submit" value="submit" onclick="comparedate()"/>
    	</form>
    </div>

3 Answers 3

2

Try it:

// You were wrong comparing only the year with the full date
var todaysYear = new Date().getFullYear();

var borndate = document.getElementById("borndate");
var form = document.querySelector("form");
// Working with forms we want to catch submit (not click) events
form.addEventListener('submit', comparedate)

function comparedate(evt) {
  // preventing default behavior (form submit in this case)
  evt.preventDefault();

  // here we translate 'string' value back to 'date' & extract year
  var yearOfBirth = new Date(borndate.value).getFullYear()
  // an alternative would be to simply parse the year off the string itself

  if (yearOfBirth >= todaysYear) {
    alert("Looks like you are to young!");
  } else {
    // submitting form programmatically
    form.submit()
  }
}
<div class="form">
  <form class="login-form" method="POST" action="main.php">
    <input type="date" id="borndate" name="borndate" required="required" />
    <input type="submit" class="button" name="button" value="submit" />
  </form>
</div>

Sign up to request clarification or add additional context in comments.

7 Comments

I actually have a submit button in my form. I want to start this script when someone submit it, so I have: <input type="submit" class="button" name="button" value="submit" onclick="comparedate()"/> and in the function, after the alert i have a return false; to stop the user to go to main.php if the date is the same or grater than the current date. However it's still letting me go to the next page
Still letting me go to main.php when I click on submit
@TheNoobUser and now?
on snippet it's working but on my code is still letting me go to main.php, i really have no clue
Sorry I forgot to tag you, i just marked your answer as correct, it was a typo on my side. Thank!
|
2

Since the variable date is the year, I assume you are trying to compare only the year.

Your code is comparing the element itself with the year. You have to do the comparison with the value of the element.

Change

if (borndate >= date)

To

if (new Date(borndate.value).getFullYear() >= date)

7 Comments

Does this work? I've not had much success with this. Could you post some code to illustrate how this would be implemented?
Ah, I see, that makes sense; the new code seems like it would work, as you're now taking a date from the value
For me is not working, it let me load into main.php even if i set the borndate to tomorrow
@TheNoobUser, do you want to compare the full date??
Just the year it's fine. Since is 2018 and I select the date of tomorrow and i have a >= it should not let me do it
|
1

I tend to find that libraries make working with dates much easier, and whilst it might be excessive to use one here for something relatively small, you may be interested to see how such an option would work:

var test = dateFns;


function comparedate(){
    var borndate = dateFns.parse(document.getElementById("borndate").value);
    var date = dateFns.parse(new Date());
    
    console.dir(date)
    console.dir(borndate)
    
    if (dateFns.isAfter(borndate, date)) {
        alert("Looks like you are to young!");
    } else {
        return true;
    }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>
<div class="form">
    <form class="login-form" method="POST" action="main.php">
        <input type="date" id="borndate" name="borndate" required="required"/>
    </form>
    <button onclick="comparedate()">Compare</button>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.