0

I'm sorry this might be duplicated post, but i really not understand how can i do this. In my system I need to generate a code(coupon) which are alphanumeric around 6 - 8 value, and if user key simply key in the code which is not under generated code(coupon) it will show error message.

i saw alot of ppl using, but i failed to applied this during import the import java.util.UUID;

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

What i have done so far only can generate a random number ( not expected result)

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

Need a guide in java code to generated random unique code and checked if the value key in if out of the generated value will show error message , Please any help would be appreciated

user3835327
  • 570
  • 1
  • 5
  • 19

2 Answers2

0

I was looking for something similar a short time ago. I wanted something like youtube id, and came across some good sites:

Take a look at: Hashids.org (see demo here)

also very good reading for this kind of solution, take a look at: Blogg - Create Youtube-Like IDs With PHP/Python/Javascript/Java/SQL

MrSimpleMind
  • 7,890
  • 3
  • 40
  • 45
0

You should replace:

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

with:

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

You missed this but your code works.

Here a demo: http://jsfiddle.net/zL1jz9c5/

Radonirina Maminiaina
  • 6,958
  • 4
  • 33
  • 60