我需要一个项目的数字和字母生成器

时间:2012-04-23 09:02:35

标签: random code-generation new-operator letters-and-numbers

我需要的数字和字母有点像这个0000A。 我正在研究一个项目,所以当我放置优惠券时,随机数字和字母将显示在我放置的优惠券ID上,但是对于每张优惠券,数字和字母都不同。 是的,我是新的编码2个月,我认为我做得很好,除了这个随机数字的事情。我正在做的例子如下。

<!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>

请有人帮帮我吗?

2 个答案:

答案 0 :(得分:1)

在java中,您可以执行以下操作:

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));

答案 1 :(得分:0)

如果您想通过JavaScript执行此操作,则可能需要类似

的功能
<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>