Javascript Function (Input Date Bigger Than Today Date)

javascript function (input date bigger than today date)

I have created a fiddle

https://jsbin.com/jidogo/edit?html,js,output

<input type="date" name="StartDate" id="userdate" onchange="TDate()" required />

JS

function TDate() {
var UserDate = document.getElementById("userdate").value;
var ToDate = new Date();

if (new Date(UserDate).getTime() <= ToDate.getTime()) {
alert("The Date must be Bigger or Equal to today date");
return false;
}
return true;
}

Only use dates greater than today

I have restructure your code. This may help you to get expected output.

$(function(){   var dateFormat = /^(?:(?:31(\/)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/)(?:0?[1,3-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$/; //date formats /      $("#check").on("click", function(){     dateValidate(dateFormat);      });   

});
function dateValidate(dateFormat){
var readDate = document.getElementById("myDate").value;
//if date is valid
if(dateFormat.test(readDate)) { var splitDate = readDate.split("/"); var day = splitDate[0]; var month = splitDate[1]; var year = splitDate[2]; var dateToday = new Date(); var dd = dateToday.getDate(); var mm = dateToday.getMonth() + 1; var yyyy = dateToday.getFullYear();
console.log('Date Today is: ' + dd + '/' + mm + '/' + yyyy);
if(day > dd) { if(month >= mm) { if(year >= yyyy) { console.log('it works - continue') return true; } else { console.log('Old Year'); return false; } } else { console.log('Old Month'); return false; } } else { console.log('Old Day') return false; } } else { console.log('Invalid Date'); $("#myDate").focus(); return false; }

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><div class="dateField"> <input type="text" id="myDate" placeholder="dd/mm/yyyy"/> <button id="check">Check</button></div>

How to check if input date is equal to today's date?

A simple date comparison in pure JS should be sufficient:

// Create date from input value
var inputDate = new Date("11/21/2011");

// Get today's date
var todaysDate = new Date();

// call setHours to take the time out of the comparison
if(inputDate.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0)) {
// Date equals today's date
}

Here's a working JSFiddle.

Comparing input type date with current date

There are number of errors on your code like getElementByID should be getElementById and you are not taking the value from input and so on. Check below snippet for reference.

function checkDate() {
var startDate = new Date(document.getElementById('biday').value); var today = new Date(); if (startDate.getTime() > today.getTime()) { alert("The first date is after the second date!"); } }
<input type="date" name="bday" id="biday" required><input type="submit" value="check" onclick="checkDate()">

throw an error if the input date is greater than today

It worked with java script also. I have called a java script from my 'form'. Here is the solution.It is working as expected by me.

Form

<form  name="purchase" id ="purchase" method="post" action="" onsubmit="return checkform()">
<div class="form-group">
<div class="input-group col-sm-4">
<div class="input-group-addon">Transaction Date     <i class="fa fa-calendar"></i></div>
<input id ="txndt" class="form-control" type="date" max="<?php echo date("Y-m-d")?>" name ="purch_date" value="<?php echo $query2['purch_date'] ?>" onblur = "txn40();" required />
</div>
</div>
</form>

Java Script

<script type="text/javascript">
function checkform()
{
var dateString = document.purchase.txndt.value;
var myDate = new Date(dateString);
var today = new Date();
if (document.purchase.txndt.value == "")
{
//something is wrong
alert('REQUIRED FIELD ERROR: Please enter date in field!')
return false;
}
else if (myDate>today)
{
//something else is wrong
alert('You cannot enter a date in the future!')
return false;
}
// if script gets this far through all of your fields
// without problems, it's ok and you can submit the form
return true;
}
</script>

Thanks to all for support.

Validate date of birth greater than today?

What I understand from your question is - "Before posting the form, you'd like to check if the DOB field has a date that must not be greater than today's date.". If this is correct, then try this, Hope this help -

<script type="text/javascript">
function checkDOB() {
var dateString = document.getElementById('id_dateOfBirth').value;
var myDate = new Date(dateString);
var today = new Date();
if ( myDate > today ) {
$('#id_dateOfBirth').after('<p>You cannot enter a date in the future!.</p>');
return false;
}
return true;
}
</script>

Javascript how to check if a date is greater than or equal to a date value

If you only want to compare Date and not Date-Time then add this line

var today = new Date();
today.setHours(0,0,0,0);

before comparison.



Related Topics



Leave a reply



Submit