Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  102] [ 7]  / answers: 1 / hits: 25421  / 12 Years ago, mon, july 30, 2012, 12:00:00

How can I check that the date entered in a textbox is less than today's date using java script?



I m using code



var currentDate_Month = new Date().valueOf().getMonth();
var currentDate_Date = new Date().getDate();
var currentDate_Year = new Date().getFullYear();
var EnterDate_Month = new Date(document.getElementById('ctl00_ContentPlaceHolder1_txtDateReceived').value).getMonth();
var EnterDate_Date = new Date(document.getElementById('ctl00_ContentPlaceHolder1_txtDateReceived').value).getDate();
var EnterDate_Year = new Date(document.getElementById('ctl00_ContentPlaceHolder1_txtDateReceived').value).getFullYear();

if(EnterDate_Year<currentDate_Year) {
if(EnterDate_Month<currentDate_Month) {
if(EnterDate_Date<currentDate_Date) {
}
}
}
else {
str += '</br>* Date should be Less than or equals to current Date.';
return false;
}


But to my surprise the current date coming in the textbox control is Sat Jun 7 2014 when viewing it by -



new Date(document.getElementById('ctl00_ContentPlaceHolder1_txtDateReceived').value).toDateString();


Why is it returning the date in this format? (the date in text box is in format dd/mm/yyyy)



thanks in advance.


More From » javascript

 Answers
12

A Date() can be initialized as



Date(mm/dd/yyyy)


Since this is the adopted method, the format of dd/mm/yyyy is not possible. The best method in your case will be to do the following



dateFields = (document.getElementById('ctl00_ContentPlaceHolder1_txtDateReceived').value.split('/')

date = Date(dateFields[2],dateFields[1]-1, dateFields[0])


This would be in the format



Date(year, month, date)


Then, you can compare the textbox date with the present date



date < Date.now()

[#83990] Friday, July 27, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
shawn

Total Points: 507
Total Questions: 103
Total Answers: 111

Location: American Samoa
Member since Fri, Aug 26, 2022
2 Years ago
;