0

I need the numbers and letters to be somewhat like this 0000-00-0000A. I am working on a project so when i place a coupon the random numbers and letter will appear on the coupon id where i place it at, but for every coupon the numbers and letter is different. And yes i am new to coding 2 months in and i think i am doing alright except for this random number thing. The examples of what i am doing is below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#coupon{
     width: 600px;
     height:auto;
     top:145px;
    background: #FFFF95;
    margin: 2px auto;
    border: 1px solid #000000;
    text-align: center;
    -webkit-border-top-left-radius:06px;
    -webkit-border-top-right-radius:06px;
    -webkit-border-bottom-left-radius:06px;
    -webkit-border-bottom-right-radius:06px;
    -moz-border-radius-topleft:06px;
    -moz-border-radius-bottomleft:06px;
    -moz-border-radius-topright:06px;
    -moz-border-radius-bottomright:06px;
    border-top-left-radius:06px;
    border-top-right-radius:06px;
    border-bottom-left-radius:06px;
    border-bottom-right-radius:06px;
    /* additional features, can be omitted */
    border:2px outset #093;
    padding:15px;
    font-size:15px;
    -moz-box-shadow: 0 0 15px #999;
    -webkit-box-shadow: 0 0 15px #999;
    box-shadow: 0 0 15px #999;
}


</style>
</head>

<body>
<div id="coupon" class="coupon">
<p>This can be the coupn area where the cupon is place and the deal can be read then printed!</p>
<div class="print-page" id="print-page">
  <p><a href=" #" onClick=" window.print()" >Print Deal</a></p>
</div>
<div class="coupon-id-number" id="coupon-id-number">0000-00-0000-A</div></div>

</body>
</html>

Please can anyone help me out?

Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • What kind of server-side technology do you use? – Thilo Apr 23 '12 at 09:05
  • 1
    what language are you wanting to use? I would expect you to be using a database to store the coupon numbers so that you can valid them later on (as single use). – TomDunning Apr 23 '12 at 09:06
  • java and iquery really... i am pretty much new to everything, and yes i have a system all worked out of storing the coupons id's. Javascript is preferred. – Thedude420 Apr 23 '12 at 09:10

2 Answers2

1

In java you would do something like:

Random random = new Random();
String couponCode = String.format("%04d-%02d-%04d-%s", random.nextInt(10000), random.nextInt(100), random.nextInt(10000), (char)(random.nextInt(26)+65));
Tor P
  • 1,351
  • 11
  • 9
  • you want to do this server side to insert into database and test that the same code doesn't allready exists, to get it client side just use Ajax to pull a new coupon code from server... – Tor P Apr 23 '12 at 09:16
  • To check if the random number is already token, you have to keep track of them in a database. – Oritm Apr 23 '12 at 09:18
0

If you would like to do this by JavaScript, you might need a function like

<script>
function genRandom() {
  var genNumber = function (length) {
        return ~~(Math.random() * length);
      },
      genChar = function () {
        var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXZ',
            index = genNumber(24);

        return chars.slice(index, index + 1);
      },
      arr = [genNumber(10000), genNumber(100), genNumber(10000), genChar()];
  return arr.join('-');
}

document.getElementById('coupon-id-number').innerHTML = genRandom();
</script>
Pat Wangrungarun
  • 516
  • 4
  • 12