I have posted this one in ASP forum and been suggested to post it here. First of all, I am not a programmer... rather designer. I know this should be simple but still need a help. I have a form with 2 date fields (date1 and date2). Something like this: <form id="form1" name="form1" method="post" action=""> <input name="date1" type="text" id="date1" /> <input name="date2" type="text" id="date2" /> <input type="submit" name="Submit" value="Submit" /> </form> What I need is to assign value of date1 to date2 IF a valid date is entered into date1... or to assign some date in the far future to date2 IF nothing is entered into date1 (needed for determining sort order upon date2... can't stay blank, unlike date1). I know this should be done through JavaScript. How do I do that?
Try this. And, "please" and "thank you" can't hurt, either. <html> <head> <script type="text/javascript"> function validate(isField){ var refDate = ""; if (isField.value == "") { refDate = new Date(); var futureStr = new Date(refDate.getFullYear(),refDate.getMonth(),refDate.getDate()+100); // 100 days ahead futureStr = futureStr.getMonth()+1+"/"+futureStr.getDate()+"/"+futureStr.getFullYear(); document.forms[0]['date2'].value = futureStr.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2"); return true; } splitDate = isField.value.split("/"); refDate = new Date(isField.value); if (splitDate[0] < 1 || splitDate[0] > 12 || refDate.getDate() != splitDate[1] || splitDate[2].length != 4 || (!/^19|20/.test(splitDate[2]))) { alert('Invalid date'); isField.value = ""; document.forms[0]['date2'].value = ""; return false; } isField.value = isField.value.replace(/^(\d{1}\/)/,"0$1").replace(/(\d{2}\/)(\d{1}\/)/,"$10$2"); document.forms[0]['date2'].value = isField.value; } </script> </head> <body> <form> Some Date: (mm/dd/yyyy) <input type='text' size='9' name='date1' onblur="validate(this)"><br> Another Date: (mm/dd/yyyy) <input type='text' size='9' name='date2' readonly><br> </form> </body> </html> Code (markup):
You're welcome. Good luck with your project. I don't look for "thanks in advance." I look for a simple, "please" and "thank you," or "thanks." It's simple, common courtesy, or rather, uncommon courtesy, because nearly all people in their 20's, 30's and even some in their early 40's haven't the slightest idea of what the phrase, "common courtesy" means, and never will. I despise the juvenile, "thx" or "tia."