如何生成6/8位

时间:2015-05-18 07:46:31

标签: java uniqueidentifier

对不起,这可能是重复的帖子,但我真的不明白我该怎么做。在我的系统中,我需要生成一个大约6 - 8值的字母数字代码(优惠券),如果用户密钥只是键入不在生成代码(优惠券)下的代码,它将显示错误消息。

我看到很多ppl使用,但我在导入导入java.util.UUID期间未能应用这个;

String uniqueID = UUID.randomUUID().toString(); //UUID method

到目前为止,我所做的只能生成随机数(非预期结果)

的Javascript

var keylist="abcdefghijklmnopqrstuvwxyz123456789";
var temp="";
function generatecoupon(plength){
temp = "";

for (i=0;i<plength;i++)
    temp+=keylist.charAt(Math.floor(Math.random()*keylist.length));

return temp;
}

function populateform(enterlength){
   document.mainfrm.COUPON.value=generatecoupon(enterlength);
}

HTML

<input type="button" value="Generate Coupon" onClick="populateform(this.form.thelength.value)">
<input type="hidden" name="thelength" size=3 value="6">     
<input name="COUPON" type="text" id="COUPON" size="20" maxlength="20">

需要java代码中的指南来生成随机唯一代码,并检查如果值生成的值中的值键将显示错误消息,请提供任何帮助

2 个答案:

答案 0 :(得分:0)

我很久以前就在寻找类似的东西。 我想要像youtube id这样的东西,并且遇到了一些不错的网站:

看看: Hashids.org (请参阅此处demo

对于这种解决方案也非常好读,请看一下: Blogg - Create Youtube-Like IDs With PHP/Python/Javascript/Java/SQL

答案 1 :(得分:0)

你应该替换:

document.mainfrm.COUPON.value=generatecoupon(enterlength);
// Uncaught TypeError: Cannot read property 'COUPON' of undefined

使用:

document.getElementById("COUPON").value=generatecoupon(enterlength);

你错过了这个,但你的代码有效。

这是一个演示:http://jsfiddle.net/zL1jz9c5/