View Full Version : Generate auto number

03-28-2009, 11:59 PM
i want to know if java script can generate auto number like 0001..in my project (payment system) i need to do resit number and print the detail about the payment..need some idea...tq

03-29-2009, 05:57 AM
Javascript can generate any random number, try searching google for Math.random().

03-29-2009, 06:48 AM
Normally a receipt number will be a sequential number rather than a random number as you've suggested, if you look at any payment system. To generate a random number in JavaScript you can make use of


but if you use it alone the number it generate may not be a usable one for you. So normally when users generate a random number they mention the range in which the newly generated random number should fall it. Like the following manner:

var rand = Math.floor(Math.random() * 101);

The above code will generate a number between 0 and 100. But if you want to make the first number 1 and second number 2 this will not work correctly as we can't control the way it generates the actual number. More if you are expecting a number like 00001 for your purpose you have to treat it as String not a number as a number 00001 is equal to 1 in JavaScript.

I would suggest to use server-side methods for storing the receipt number if you want to use it in a more efficient manner.