Math.random integration


#1

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.


#2

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


Adding unique identifier to lead
Adding unique identifier to lead
#3

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


#4

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


#5

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


#6

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


#7

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


#8

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 :slight_smile: )