Passing value of one field to another

Discussion in 'JavaScript' started by ahcj0505, Jun 5, 2007.

  1. #1
    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?
     
    ahcj0505, Jun 5, 2007 IP
  2. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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):
     
    Mike H., Jun 5, 2007 IP
  3. ahcj0505

    ahcj0505 Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks Mike... just what I needed. "Thanks in advance" has never worked for me.
     
    ahcj0505, Jun 5, 2007 IP
  4. Mike H.

    Mike H. Peon

    Messages:
    219
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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."
     
    Mike H., Jun 5, 2007 IP