How to auto generate random number and fill in a hidden form field

  • 24 May 2015
  • 1 reply
  • 60 views

I would like to figure out how to auto generate a random number and then autofill the resulting number into a hidden form field . I’m trying to create a page ofering an aggressive discount by issuing a unique promo code and attaching it to the users email address so I don’t have a bunch of people trying to get a discout using a universal coupon code. The page URL is  http://unbouncepages.com/the-works-discount?campaign=01_2015&offer=THEWORKS


1 reply

Hi Mike,

I’d use Javascripts math.random(); 

Use the following function inline or referenced from a .js file.

function randomInt(min,max) {

  return Math.floor(Math.random()*(max-min+1)+min); }

In the onSubmit section for your submit button put this…

document.getElementById("WhateverYourHiddenFieldsCalled").value = randomInt(1,100);

Obviously change the bit in the quotes that say “WhateverYourHiddenFieldsCalled” to the actual name of your field, you can get this by using inspect element in Chrome or Firefox.

That should place a random number in the hidden field before submitting the form and you can then track the codes from the submissions or whatever CRM you  use etc.

There are shed loads of ways of doing it,  but I hope that helps a little.

Cheers

Stuart.

Reply