2012-04-23 78 views
0

我需要的数字和字母有点像这样的0000-00-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> 

请任何人都可以帮我吗?

+0

什么样的服务器端技术你使用? – Thilo 2012-04-23 09:05:32

+1

你想用什么语言?我希望您使用数据库来存储优惠券号码,以便以后可以验证它们(作为单次使用)。 – TomDunning 2012-04-23 09:06:44

+0

java和iquery真的...我几乎是新鲜的一切,是的,我有一个系统都存储优惠券ID的工作。 Javascript是首选。 – Thedude420 2012-04-23 09:10:48

回答

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

你想做这个服务器端插入到数据库和测试,相同的代码不allready存在,为了让它客户端只是使用Ajax从服务器拉新的优惠券代码... – 2012-04-23 09:16:21

+0

要检查是否随机数已经是令牌,你必须在数据库中跟踪它们。 – Oritm 2012-04-23 09:18:33

+0

对于优惠券代码,我会使用SecureRandom或UUID。 – Thilo 2012-04-23 09:22:33

相关问题