Guyz, I have 2 textboxes (PHP script) i need to validate them using Javascript. one textbox is to type date.. but the user should not be able to type a future date and press submit. i wrote a javascript validation : <script language="JavaScript"> <!-- function CompareDates(str1,str2) { var dt1 = parseInt(str1.substring(0,2),10); var mon1 = parseInt(str1.substring(3,5),10); var yr1 = parseInt(str1.substring(6,10),10); var dt2 = parseInt(str2.substring(0,2),10); var mon2 = parseInt(str2.substring(3,5),10); var yr2 = parseInt(str2.substring(6,10),10); var date1 = new Date(yr1, mon1, dt1); var date2 = new Date(yr2, mon2, dt2); if(date2 < date1) { return false; } else { return true; } } function validate() { var str2 = document.getElementById("date").value; var str1 = "23/10/2010"; if (document.frm.date.value=="") { alert("Please enter date"); document.frm.date.focus(); return false; } else if (document.frm.number.value=="") { alert("Please enter number"); document.frm.number.focus(); return false; } else if (eval(document.frm.number.value)<=0) { alert("Please enter number positive"); document.frm.number.focus(); return false; } else if (CompareDates(str1,str2)) { alert("To date cannot be greater than from date"); document.frm.date.focus(); return false; } else return true; } //--> </script> Code (markup): This works perfectly but the problem is validation takes current date from : var str1 = "23/10/2010"; Code (markup): i need that to be the system date so i dont have to change the str value everytime.. any help? Thanks in advance
Just replace: var str1 = "23/10/2010"; Code (markup): with: var date = new Date(); var str1 = date.getDate() + '/' + (date.getMonth() + 1) + '/' + date.getFullYear(); Code (markup):