www.webdeveloper.com
Results 1 to 3 of 3

Thread: If, Else, If Else Help?

  1. #1
    Join Date
    Aug 2008
    Posts
    31

    If, Else, If Else Help?

    Hello, I have this script that takes input from three form boxes and inserts it into a mailto href. but when i try to modify it at all it stops working. what i want to do is have if, else, and if else statements that if a user doesnt fill in a box it alerts them what box they didnt fill in and goes back to the input boxes so they can fill it in. ive tried many times to do this but it seems that every time i touch it it messes up

    Here is the code

    Code:
    <script type="text/javascript">
    function email (form) {
    var address = form.address.value;
    var name =form.name.value;
    var username = form.username.value;
    today = new Date();
    BigDay = new Date("December 25, 2008")
    msPerDay = 24 * 60 * 60 * 1000 ;
    timeLeft = (BigDay.getTime() - today.getTime());
    e_daysLeft = timeLeft / msPerDay;
    daysLeft = Math.floor(e_daysLeft);
    e_hrsLeft = (e_daysLeft - daysLeft)*24;
    hrsLeft = Math.floor(e_hrsLeft);
    minsLeft = Math.floor((e_hrsLeft - hrsLeft)*60);
    document.location.href = "mailto:" + address + "?subject=Whatever&body=Hello " + name + ",<br><br>" + daysLeft + " days, " + hrsLeft + " hours and " + minsLeft + " minutes left. This message was sent to you from " + username + "
    }
    </script>
    Code:
    <form name="info" method="get">
    <input type="text" name="address" value="Recipiant's E-mail Address" onclick="this.value='';this.onclick=null;" /><br />
    <input type="text" name="name" value="Recipiant's Name" onclick="this.value='';this.onclick=null;" /><br />
    <input type="text" name="username" value="Your Name" onclick="this.value='';this.onclick=null;" /><br />
    <input type="button" name="button" value="Tell A Friend" onclick="email(this.form)" />
    <input type="button" name="button" value="Reset" onclick="javascript:if(confirm('Are you sure you want to reset the info?')) { document.info.reset(); }"/>
    </form>

  2. #2
    Join Date
    Apr 2008
    Location
    UK
    Posts
    76
    Code:
    <script type="text/javascript">
    function email (form) {
    	var address = form.address.value;
    	var name =form.name.value;
    	var username = form.username.value;
    	if(address.replace(/^\s+|\s+$/g, '') == "") {
    		alert("Please Complete the Address Field");
    		
    		form.address.focus();	
    	} else if(name.replace(/^\s+|\s+$/g, '') == "") {
    		alert("Please Complete the Name Field");
    		
    		form.name.focus();	
    	} else if(username.replace(/^\s+|\s+$/g, '') == "") {
    		alert("Please Complete the Username Field");
    		
    		form.username.focus();	
    	} else {
    		today = new Date();
    		BigDay = new Date("December 25, 2008")
    		msPerDay = 24 * 60 * 60 * 1000 ;
    		timeLeft = (BigDay.getTime() - today.getTime());
    		e_daysLeft = timeLeft / msPerDay;
    		daysLeft = Math.floor(e_daysLeft);
    		e_hrsLeft = (e_daysLeft - daysLeft)*24;
    		hrsLeft = Math.floor(e_hrsLeft);
    		minsLeft = Math.floor((e_hrsLeft - hrsLeft)*60);
    		document.location.href = "mailto:" + address + "?subject=Whatever&body=Hello " + name + ",<br><br>" + daysLeft + " days, " + hrsLeft + " hours and " + minsLeft + " minutes left. This message was sent to you from " + username
    	}
    }
    </script>
    
    <form name="info" method="get">
    <input type="text" name="address" value="Recipiant's E-mail Address" onclick="this.value='';this.onclick=null;" /><br />
    <input type="text" name="name" value="Recipiant's Name" onclick="this.value='';this.onclick=null;" /><br />
    <input type="text" name="username" value="Your Name" onclick="this.value='';this.onclick=null;" /><br />
    <input type="button" name="button" value="Tell A Friend" onclick="email(this.form)" />
    <input type="button" name="button" value="Reset" onclick="javascript:if(confirm('Are you sure you want to reset the info?')) { document.info.reset(); }"/>
    </form>

  3. #3
    Join Date
    Aug 2008
    Posts
    31
    thanks soo much! works like a charm

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles