Post

# How to Create a Unique Number in Javascript for Payment Gateway?

## Introduction

In this article, we learn how to create a unique number in JavaScript for a payment gateway; you can use a combination of techniques depending on your specific requirements. Here's an example of generating a unique number.

Example

Here 1st, we create a function as "getMonthsInLet" for getting a month in the letter means if January, then "A"; if February, then "B" accordingly as below.

Code

`````` function getMonthsInLet(mid) {
if (mid == 0) {
return "A";
} else if (mid == 1) {
return "B";
} else if (mid == 2) {
return "C";
} else if (mid == 3) {
return "D";
} else if (mid == 4) {
return "E";
} else if (mid == 5) {
return "F";
} else if (mid == 6) {
return "G";
} else if (mid == 7) {
return "H";
} else if (mid == 8) {
return "I";
} else if (mid == 9) {
return "J";
} else if (mid == 10) {
return "K";
} else if (mid == 11) {
return "L";
}
}``````

After that, use the below code for generating the unique number and assign it to this in leble or hiddenfield.

Code

``````var Date1 = new Date();
var getMonLet = getMonthsInLet(Date1.getMonth());

var RandNo = 'PG' + Date1.getDate().toString().padStart(2, "0") + getMonLet + Date1.getFullYear().toString().substr(-2) + Date1.getHours() + Date1.getMinutes() + Date1.getSeconds() + Date1.getMilliseconds();
\$('.TransNo').text(RandNo);
\$('.hdnTransNo').val(RandNo);``````

Input

Output

Here we find the below output. You can try this code in JSfiddle (https://jsfiddle.net/) also.

## Conclusion

You can use the above code in the button click event or documents ready function when the page will load. This is very useful as a payment Gateway for a transaction. It's working very perfectly.