Paul McFedries' Web Home


JavaScript Programs

Restricting the Number of Times a User Can Submit a Form

This page shows you how to use JavaScript cookies to restrict the number of times a user can submit a form. Here is a test form (the maximum number of times you can submit it is 2):


Just testing:

Here's the <FORM> tag I'm using:
<FORM 
   ACTION="http://www.mcfedries.com/scripts/formtest.asp"
   METHOD="POST" 
   onSubmit="return validate(this)">

Notice how it includes an onSubmit attribute that tells the browser to run the validate( ) function when the user submits the form.

The JavaScript below shows you the validate( ) function as well as the various cookie-related functions.. Three things to note:

  • Use the maxSubmits variable to set the maximum number of times the user is allowed to submit the form.
  • The name of the cookie is TotalSubmissions. If you plan on using this code in multiple forms, be sure to use a unique cookie name in each form.
  • I've included a button that enables you to reset the cookie to 0. This is for testing purposes only! You should delete this as soon as you're finished testing the form.
To use this script, copy everything between (and including) the <SCRIPT> and </SCRIPT> tags, and insert it between your page's </HEAD> and <BODY> tags:
<SCRIPT LANGUAGE="JavaScript">
<!--
//
// Use the following variable to specify the maximum 
// number of times the user can submit the form
//
var maxSubmits = 2

function validate(frm)
{
    //
    // Get the "TotalSubmissions" cookie
    //
    var totalSubmits = eval(GetCookie('TotalSubmissions'))
    //
    // If it hasn't been set, initialize it to 0
    //
    if (totalSubmits == null)
        totalSubmits = 0
    //
    // See if totalSubmits is greater than or equal to maxSubmits
    //
    if (totalSubmits >= maxSubmits)
    {
        //
        // If so, don't submit the form
        //
        alert("This form can't be submitted any more.")
        return false
    }
    else
    {
        //
        // Otherwise, increment and save the cookie and submit the form
        //
        totalSubmits = totalSubmits + 1
        BakeIt(totalSubmits, "TotalSubmissions")
        return true
    }
}

//
// This function is for testing purposes only!
//
function ResetCounter()
{
    BakeIt(0, "TotalSubmissions")
}

function BakeIt(cookieData, cookieName) 
{
    //
    // Use this variable to set the number of days after which the cookie will expire
    //
    var days = 999;
    //
    // Calculate the expiration date
    //
    var expires = new Date ();
    expires.setTime(expires.getTime() + days * (24 * 60 * 60 * 1000)); 
    //
    // Set the cookie
    //
    SetCookie(cookieName, cookieData, expires);
}

function SetCookie(cookieName, cookieData, expireDate) 
{
    document.cookie = cookieName + "=" + escape(cookieData) + "; expires=" + expireDate.toGMTString();
}    

function GetCookie(name) 
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
            return GetCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return null;
}

function GetCookieVal (offset) 
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(offset, endstr));
}
//-->
</SCRIPT>

Copyright © 1995-2008 Paul McFedries and Logophilia Limited