Math.random integration

  • 15 February 2016
  • 10 replies
  • 77 views

how exactly do I integrate the javascript math.random function into my form submission? I want to create a unique confirmation number to auto-populate in a hidden field when the form is submitted so that I can track a specific referral to the initial form submission within my CRM software.


10 replies

Userlevel 7
Badge +3

Hi Garrett, 

First, welcome to the Unbounce community. 

I just played around a bit with the math.random function and managed to get it working. 
(Please keep in mind this hasn’t been thoroughly tested so do your due diligence and test across browsers/devices)

&nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; &nbsp; var randomNum = '';<br />&nbsp; &nbsp; &nbsp; randomNum += Math.round(Math.random() * (100000 - 10000) + 10000);<br />&nbsp; &nbsp; &nbsp; var elem = document.getElementById("random_number").value = randomNum;<br />

The script above would generate a number between 10,000 and 100,000. You can adjust that part based on your particular needs. 

  • You have to change the “random_number” part from the script above to the name of your hidden field. 

  • If a visitor submits the form a 2nd time without reloading the page, they would get the same random number. This is due to the way javascript only loads once per page. 

Let us know how it works out. 

Best,
Hristian

Hristian -

It looks like the function works, thank you much.

Lastly, is there a way to reflect the generated number on the Form Confirmation Dialogue? I have read here that is not able to be done, however some time has passed and I am curious if it has the ability to be implemented.

Thanks,
Garrett

Userlevel 7
Badge +3

Hi Garrett, 

I’m glad the code snippet was helpful. 

To your follow up questions… Yes, you can show the generated number on the Thank You popup. 

However, I don’t a script handy at the moment. 

I know you can use the Dynamic Text Replacement feature of Unbounce to display field values on the Thank You page but I’m not sure how it would handle a hidden field. 

Give it a try and if you still can’t get it to display the random number, I can write you a custom script. 

Feel free to shoot me an email at: h [at] revise [dot] cc

Best,
Hristian

Userlevel 6
Badge +4

Values from hidden fields can also be passed using DTR mentioned above 😉

Hi Hristian,

I also tried to add this however I have yet been unsuccessful. For us slow people who are just starting can I please confirm step by step:

I should add this script globally to all my pages
add a hiddden variable called: random_number

And it should work. Or am I missing something?

Thanks,

Brendan

Userlevel 7
Badge +3

Hi Brendan,

You should add the script only to the pages you want to use it on. No need to load additional scripts if you are not using them.

Also, you need to add a hidden field and not a hidden variable.

Last but not least, here is a link to the script. The formatting on the above script seems to be off due to the change of platforms here a few months back.

Best,
Hristian

Hi Hristian,

many thanks for your reply. I think it was the formatting that fixed it. (PS - I wanted to use random_number on every page save one so I took the lazy way out and applied it to all 🙂 )

Badge

Hi,

 

Was anyone able to figure out Hristian’s original request? I added a hidden field in my lightbox form and then also added the javascript he described here (https://gist.github.com/hristiank/36e98366fa06285a79b656ce6b351f5e) to my main page (entry landing page) in the javascript in the HEAD of the code.

 

That should be all right? I see the field in my leads listview but all my leads I push through as a test do not have a random number associated… Please help!

Userlevel 4
Badge

Hi,

 

Was anyone able to figure out Hristian’s original request? I added a hidden field in my lightbox form and then also added the javascript he described here (https://gist.github.com/hristiank/36e98366fa06285a79b656ce6b351f5e) to my main page (entry landing page) in the javascript in the HEAD of the code.

 

That should be all right? I see the field in my leads listview but all my leads I push through as a test do not have a random number associated… Please help!

Hi @calvinylin,

 

I believe the issue here is that the script needs to be placed in the javascript section of the lightbox, not the main page.

 

I ran a test using the provided script, placed it in the lightbox javascript and set the placement to ‘Before Body End Tag’. The random number was showing in my hidden field - here is a short video I made of the test.

 

Hopefully this does the trick 👍

Badge

@Oliver_Lord oh my goodness you’re a godsend!! That completely fixed it. Thank you so much for reviving this thread and providing a video instruction.

 

Reply