0

For some reason I have been unable to find a quick solution to this. All I am trying to do is add dashes to a "randomly" generated 32 character string in Javascript like so:

From: AA681EBC64F642B1AFA95EE2A5D87350
To: AA681EBC - 64F6 - 42B1 - AFA9 - 5EE2A5D87350 (added spaces for clarity)

I assume the code would look something along the lines of

String.format(randomString, {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx});

but in Javascript

Current code (using angular-random-string.js):

var value = angular.uppercase(randomString(32));
shredMER
  • 227
  • 2
  • 16
  • How do you create random string? Could you post some code? – sinisake Sep 30 '15 at 22:38
  • 1
    What have you tried? You say you can't find a solution. Does that mean you've tried to write one? If so, what issues did you encounter? –  Sep 30 '15 at 22:39
  • are you really asking this dup http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript – ergonaut Sep 30 '15 at 22:41
  • Just attached what I'm using. I have been looking for a similar example that I posted and have not found anything. A way to define characters and then divide by dashes. I will try that example ergonaut. Looks like what I need and would actually allow me to remove the angular-random-string.js add on – shredMER Sep 30 '15 at 22:45

2 Answers2

1

You can use something like this: (fiddle here)

var code = "AA681EBC64F642B1AFA95EE2A5D87350";
var test = "AA681EBC-64F6-42B1-AFA9-5EE2A5D87350"
var fCode = code.substring(0, 8) + "-" + code.substring(8, 12) + "-" + code.substring(12, 16) + "-" + code.substring(16, 20) + "-" + code.substring(20);

// test
console.log(test === fCode);
Tivie
  • 18,864
  • 5
  • 58
  • 77
0

Using your existing function you could do this:

var parts = [randomString(8), randomString(4), randomString(4), randomString(4), randomString(12)];
var value = parts.join('-').toUpperCase();
Phil
  • 1,996
  • 1
  • 19
  • 26