How to create a random generated number to my form?

Hi, I hope someone will notice this. I want to create a random generated number in my form if someone is fill up form, like a ID number, cause I can’t edit the default ID database created.

2 Likes

Hi geovel,

Before I answer that I have to point out that random numbers are not uniq. Of course, the larger your range is, there’s less chance to get duplication. But if you go for it anyway - when generating a new number, don’t forget to make sure that you don’t already have it as an ID in your database (and if so, generate a new one).

You can use ’ Math.random ()’ to get a random number between 0 to 1 (e.g. 0.7362768324).
Have a look here for ideas on how to get this number to be an integer, and even set a range to that integer.
Also, here 's some example on how to generate a UUID string.

Liran.

2 Likes

Thank you Liran! would you please post of a sample code? Thank you again for the help.

1 Like

I’m also interested in changing the default ID which is very unaesthetic.
If anyone has a solution.

My items’ URL would be " /{ID}/{Title} "

I’d like the id to be something like “15795169” and the not default Mac-style address.

Thanks !

1 Like

bump

Hi Tristan,

Here are some threads/info that may help you:

https://www.totallycodable.com/forum/codes-for-templates/unix-time-stamp-code-epoch

https://www.wix.com/code/home/forum/questions-answers/solved-i-need-a-help-of-a-wix-expert

Once you create a random number, you can use that for your URL instead of the ID (since the ID cannot be modified in any way).

Hi !

Thanks, I’ve read all the articles, I tried it for myself and it works :

 $w.onReady(function () { 
  unixCode(); 
  }); 
 function unixCode (){ 
  var timeStamp = Math.floor(Date.now()/1000);
  $w('#yourElement').value = timeStamp.toString(); 
 } 

Only is there a way to achieve this without having to put an input element on the page ? I would like this to be a ‘backend’ feature, so no one can see it happening. And hiding it isn’t really an option since Goodle doesn’t appreciate hidden content.

Thanks !

PS : I saw 2 or 3 different types of code (including yours) what are the differences ?

1 Like

Hi! Is there possibility to generate a unique number for wix-form database?

Install a NPM called uuid , it has a method that creates Universally Unique Identifier.

Then you just import and use the method below:

import { v4 as uuid } from 'uuid';


const newId = uuid() //this generates a uuid string