Forum Discussion

MikailKote's avatar
MikailKote
Qrew Trainee
5 years ago

IOL Refresh a specific field. is it possible?

Okay so i added a sort of RNG which we need 15 characters of (this is another issue i have i cannot for the life of me find a way to have it only generate 15 characters this is the formula i have at the moment for the rng)
function makePasswd() {
var passwd = '';
var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
for (i = 0; i<16; i++) {
var c = Math.floor(Math.random() * chars.length + 1);
passwd += chars.charAt(c)
}
return passwd;
}

$("#_fid_18").val(makePasswd());
ā€‹

i already tried changing the i<16 to i=16 and that basically crashed the form. it wouldn't load. but now it sometimes generates 15 or 17 or 14 its always different i need it to be 15 characters at all times.

I kinda gave up trying to get this to work so i'm wondering if there's a way to refresh a specific field but not refreshing the entire page. So that people can just spam click this button until they get a 15 character random code. also i've got a Length formula for the field its generating on so to make sure its 15 (Sadly it only works if the value is refreshed by Highlighting the value, cutting, clicking out of it and pasting it back in)

I just tried adding this into a rich text field

but it ended up just refreshing the entire page.

I rarely ask for help lol, unless i'm totally stuck.



------------------------------
Thanks,
Mikail Kote
------------------------------

2 Replies

  • If you can fix the function to always generate a random string that is 15 characters long, you won't need to refresh the field, right?

    Here's how you can fix the function:
    In your for loop, you need to have i<15 instead of i<16. This is because you start at i=0 and i is incremented only after the end of the looped code is reached. On the 15th iteration, i=14 so you want to stop the loop after that iteration.

    The reason you're seeing variable string lengths is because of this formula:
    Math.floor(Math.random() * chars.length + 1);
    By adding 1, you can occasionally generate a number that will cause you to look for a position in the string that is actually outside of the string (e.g. you're looking for the 37th character in a 36 character string). Math.random generates a random number between 0 and 1 that includes 0 but DOES NOT INCLUDE 1. Thus, the maximum number you can generate with the edited formula is 35 due to Math.Floor, but this is ok because charAt is 0-indexed! The last character in the string would be obtained by charAt(35) and so there is no reason to add 1 at the end.

    If you make both of these changes, I believe you should be able to consistently generate 15 character random strings. If you want to get fancy, you can adjust the code to accept a length parameter and set up your function based on that so that you could later change to an 8 character string or a 37 character string if necessary. Disclaimer: I did not test this code with the changes I propose above, but if memory serves, this should get you the correct output.

    ------------------------------
    -Tom
    ------------------------------
    • MikailKote's avatar
      MikailKote
      Qrew Trainee
      That worked perfectly Thank you :D 

      Changed it to: 
      function makePasswd() {
        var passwd = '';
        var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        for (i = 0; i<15; i++) {
          var c = Math.floor(Math.random() * chars.length + 0);
          passwd += chars.charAt(c)
        }
        return passwd;
      }
      
      $("#_fid_18").val(makePasswd());ā€‹


      ------------------------------
      Mikail Kote
      ------------------------------