CoderZone.org

Category: >> JavaScript >> Download only once Bookmark and Share

<< lastnext >>

Snippet Name: Download only once

Description: Here is a form that asks for Name and Email address. When the form is submitted the Email address is verified first.

If you put in a Name and a correct Email address, an anchor element is displayed allowing the user to download the file, two cookies are created storing the Name and Email address.

If the user goes BACK in the browser and tries to download the file again, the program checks to see if the cookies exist.
If they do exist, then a message is displayed telling the user that they can only download the file once.

Comment: (none)

Author: CoderZone
Language: JAVASCRIPT
Highlight Mode: HTML4STRICT
Last Modified: February 27th, 2009

<html>
<head>
<style>
td
{
 text-align:right;
 padding-right:3px;
 }
#a1
{
 display:none;
 }
</style>
<script>
function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}
function validate(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      alert('Invalid Email Address');
      return false;
   }
  return true;
}
function download_file()
{
 var obj_name = document.getElementById("name");
 var obj_email = document.getElementById("email");
 var cookie_1 = obj_name.name;
 var cookie_2 = obj_email.name;
 if(!document.cookie)
 {
  if(obj_name.value&&validate(obj_email.value))
  {
   var a1 = document.getElementById("a1");
   var theForm = document.getElementById("theForm")
   a1.style.display="inline";
   theForm.style.display="none";
   createCookie(cookie_1,obj_name.value,"");
   createCookie(cookie_2,obj_email.value,"");
   }
  }
 else
 {
  if(readCookie(cookie_1)&&readCookie(cookie_2))
  {
   var file_name = document.getElementById("a1").href.split('/');
   alert("You are only allowed to download file "+file_name[file_name.length-1]+" once.");
   }
  }
 return false;
 }
</script>
</head>
<body>
<form id="theForm" onsubmit="return download_file();">
<table>
 <tr>
  <td><label for="name">Name: </label><input id="name" name="name"></td>
 </tr>
 <tr>
  <td><label for="email">Email: </label><input id="email" name="email"></td>
 </tr>
 <tr>
  <td><input type="submit" value="Submit"></td>
 </tr>
</table>
</form>
<a id="a1" href="http://someAddress.com/someFile.mp3">Download someFile.mp3</a>
</body>
<html>
 
There haven't been any comments added for this snippet yet. You may add one if you like.  Add a comment 
© coderzone.org | users online: 8