Jump to content

TomC

Members
  • Content Count

    7
  • Joined

  • Last visited

Community Reputation

0 Neutral

About TomC

  • Rank
    Newbie
  1. Hi All - sorry for slow response.aalbetski - your script a while lot less crude then my effort. And sorry about the two T's in the original list! I had been mucking about with it and left extra char in.justsomeguy - thanks, this looks neat and works well, though to be honest i cant figure out how it works!jesh - good point. In fact I will be end user, but makes sense to ensure char list is > string lengththanks all
  2. With a bit of help from this forum, I now have a JS generating a random string of characters, of varying length. However, I now realise that for my puposes, I dont want to duplicate any characters within the string, ie I want all characteres to be different.I realise that avoiding duplicating the entire string is an"old chestnut" but is there any way to solve my problem? I think I can see a method using a number of if / else statements, by creating single characters and reexecuting in the event of duplication, but this would be truly horrible.The script I'm using is:function randomString() { var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZ"; var string_length = 1; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); } return randomstring;}Grateful for any tips,
  3. TomC

    Js Random String

    Ok, got that. Actually I now have a new problem, in that I dont want to repeat any characters within any given string. I think this may prove a bit more tricky to solve - I'd be interested in your comments. Have posted fresh query,Thanks again.
  4. TomC

    Js Random String

    OK justsomeguy, that does it fine! Much better than paracetamol.Big thanks for to all for your helpful advice. Still cant help wondering why I couldnt make the other solution work....
  5. TomC

    Js Random String

    OK, I feely admit to being a dunce - but please believe I have spent hours on this, and now have quite an impressive headache!I now have:<script language="javascript" type="text/javascript">function randomString() { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; var string_length = 8; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); } document.randform.randomfield.value = randomstring;}window.onload = randomString;</script>With this in the head, I get "document.randform etc is null, or not an object"If I take that line out (as there is now no randform in the body), the script doesnt call. If I leave it in, I still cant get a result no matter where I put the line "window.onload = randomString;"I also tried using, in the body: <script type="text/javascript">document.write(randomString())</script>but this doesn't seem to work either. I'm obviously still wide of the mark, but I cant work out why. Grateful for any further help please,
  6. TomC

    Js Random String

    hi Deirdre's Dad.Sorry to be a bit dense. Do you mean I should leave my script exactly as is, and add window.onload = randomString outside of the function brackets? Also I've placed all the script inside the head. Is that correct?thanks
  7. TomC

    Js Random String

    Hi all - hope somebody can help with this. I'm trying to create a random string of text with determined characters. The script below works great, but uses a form to call it. I want the string to show on page load, but cant work out how to alter the JS to make that happen. I thought if I just omitted the function and form lines it should call immediately, but this doesnt seem to work. <script language="javascript" type="text/javascript">function randomString() { var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz"; var string_length = 8; var randomstring = ''; for (var i=0; i<string_length; i++) { var rnum = Math.floor(Math.random() * chars.length); randomstring += chars.substring(rnum,rnum+1); } document.randform.randomfield.value = randomstring;}</script>PS I have worked throught the tutorials, but still cant sort it. Thanks
×
×
  • Create New...