I want to validate for From Date & To Date (ie., From Date should not be greater than To Date & also From Date & To Date should not be greater than current date) & Dates are in us format
From Date & To Date Validation in javascript
Collapse
X
-
Tags: None
-
-
This is the code which i developed & the date will be in us date
Code:var objFromDate = document.getElementById("fromdate").value; var objToDate = document.getElementById("todate").value; var date1 = new Date(objFromDate); var date2 = new Date(objToDate); var date3 = new Date(); var date4 = date3.getMonth() + "/" + date3.getDay() + "/" + date3.getYear(); var currentDate = new Date(date4); if(date1 > date2) { alert("fromdate should be less than todate"); return false; } else if(date1 > currentDate) { alert("From Date should be less than current date"); return false; } else if(date2 > currentDate) { alert("To Date should be less than current date"); return false; }Comment
-
How can i edit the above code b'coz there is a little issue in current date. I want to add the new code which works perfectly for my requirementComment
-
This is the code for date comparison
Code:var objFromDate = document.getElementById("fromdate").value; var objToDate = document.getElementById("todate").value; var FromDate = new Date(objFromDate); var ToDate = new Date(objToDate); var valCurDate = new Date(); valCurDate = valCurDate.getMonth()+1 + "/" + valCurDate.getDate() + "/" + valCurDate.getYear(); var CurDate = new Date(valCurDate); if(FromDate > ToDate) { alert(fromname + " should be less than " + toname); return false; } else if(FromDate > CurDate) { alert("From date should be less than current date"); return false; } else if(ToDate > CurDate) { alert("To date should be less than current date"); return false; }Comment
-
Two things I should point out:
1. There's no need to create two objects for the current date. When you create a new Date object, it's automatically set to today's date:
2. You should validate the actual string for the "from" and "to" dates that they are valid dates, e.g. with regular expressions.Code:var today = new Date();
PS. please use code tags around your code: [code]like this[/code] It makes it a lot easier to read.Comment
-
-
you can check this using the following jquery code
for more info refer this : check entered date is greater than current dateCode:var EnteredDate = document.getElementById("txtdate").value; //for javascript var EnteredDate = $("#txtdate").val(); // For JQuery var date = EnteredDate.substring(0, 2); var month = EnteredDate.substring(3, 5); var year = EnteredDate.substring(6, 10); var myDate = new Date(year, month - 1, date); var today = new Date(); if (myDate > today) { alert("Entered date is greater than today's date "); } else { alert("Entered date is less than today's date "); } }Comment
-
Code:if (iForm.DiddfromDate.value == "") { alert(" Please enter a value"); iForm.DiddfromDate.focus(); return false; } if (iForm.DiddtoDate.value == "") { alert(" Please enter a value"); iForm.DiddtoDate.focus(); return false; } try { var d1 = iForm.DiddfromDate.value.substr(0, 2); var m1 = iForm.DiddfromDate.value.substr(3, 2); var y1 = iForm.DiddfromDate.value.substr(6, 4); var StrDate = m1 + "/" + d1 + "/" + y1; var d2 = iForm.DiddtoDate.value.substr(0, 2); var m2 = iForm.DiddtoDate.value.substr(3, 2); var y2 = iForm.DiddtoDate.value.substr(6, 4); var EndDate = m2 + "/" + d2 + "/" + y2; var startDate = new Date(StrDate); var endDate = new Date(EndDate); if (startDate > endDate) { alert('To date should be greater than From date.'); iForm.DiddfromDate.value = ''; iForm.DiddtoDate.value = ''; iForm.DiddfromDate.focus(); return false; } } catch (e) { alert(e.Description); } return true; }Last edited by Rabbit; Mar 4 '14, 04:30 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.Comment
Comment