Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with charAt (i think)

    Complete newbie here and with javascript. Having to write a small program, within this the user has to put in a password. Once ok is clicked it needs to be displayed, with only the 1st and last character showing with the rest replaced with an asterisk *. Ive got it so it adds one * but am stuck.

    Can anyone help at all? Thanks

  • #2
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    It might help if you post what you have so far.

    I would use charAt() to return the first character (0) and the last character (string.length-1). Display (string.length-2) asterisks in between them.

  • #3
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    document.write('<BR>' + 'Password: ' + passwordLength.charAt(0) + character + passwordLength.charAt(length + 1))

    Thats the line what ive got to display the first and last character and then display my variable 'character', which i defined as *. Does that help. What it needs to do is display the rest of the password, as its got to be at least 7 characters and less than 15! Does that make sense!

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Code:
    passwordLength.charAt(passwordLength.length + 1)
    charAt(length + n) does not exist for n >= 0

    You probably meant to write
    Code:
    passwordLength.charAt(passwordLength.length - 1)
    Trinithis

  • #5
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I meant to put that but as im new its not going to be exactly right. The thing is the program runs and does what i want it to do, except for the problem i had in my first post.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Here's another way:-

    <script type = "text/javascript">

    var pwd = "abcdefghi";
    var pwdlen = pwd.length;
    if (pwdlen <7 || pwd.length > 15) {
    alert ("Invalid password! Must be 7 - 15 characters");
    return false;
    }

    var pwd1 = pwd.match(/^./); //first character
    var pwd3 = pwd.match(/.$/); // last character
    var pwd2 = pwd.replace (/./g, "*"); // change to asterisks
    var pwd2 = pwd2.replace(/\*\*/, ""); // delete two asterisks
    var xpwd = pwd1 + pwd2 + pwd3; // concatenate password

    alert (xpwd); // delete after testing

    </script>

  • #7
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the replies, i can understand where youre going. Forgot to say originally, that got to use the basics really, so really IF statements, loops, etc, does that make sense.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    Well, here is yet another way:-

    <script type = "text/javascript">

    function obscure() {
    var pwd = "abcdefghi";
    var pwdlen = pwd.length;
    var xpwd = "";
    if (pwdlen <7 || pwd.length > 15) {
    alert ("Invalid password! Must be 7 - 15 characters");
    return false;
    }

    var a = pwd.charAt(0); // first character
    var z = pwd.charAt(pwdlen-1); // last character

    for (var i = 1; i < pwdlen-1; i++) {
    xpwd += "*"; // change intermediate characters to *
    }
    xpwd = a + xpwd + z; // concatenate first + *** + last
    alert (xpwd); // delete after testing
    }

    </script>

    You must call the function by a button or by using onchange = "obscure()" in the password input field.
    Last edited by Philip M; 12-18-2007 at 10:01 AM.

  • #9
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I kind of get what that does, but still cant use functions. Basically the question says using your ability to use conditionals, repetition structures and string handling? Does that narrow it down, thanks, sorry not being much help am i?

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,303
    Thanks
    203
    Thanked 2,562 Times in 2,540 Posts
    You should not ask us to do your homework for you. It amounts to cheating your tutor, your fellow-students and ultimately yourself. Are you sure that you have paid sufficient attention in class?

    Let this suffice:-

    <script type = "text/javascript">

    var pwd = "abcdefghij";
    var pwdlen = pwd.length;
    var xpwd = "";
    if (pwdlen <7 || pwd.length > 15) { // conditional
    alert ("Invalid password! Must be 7 - 15 characters");
    }

    var a = pwd.charAt(0); // first character string handling
    var z = pwd.charAt(pwdlen-1); // last character

    for (var i = 1; i < pwdlen-1; i++) { // repetition structure
    xpwd += "*"; // change intermediate characters to *
    }
    xpwd = a + xpwd + z; // concatenate first + *** + last // string handling
    alert (xpwd); // delete after testing

    </script>
    Last edited by Philip M; 12-18-2007 at 09:48 PM.

  • #11
    New to the CF scene
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Im not asking you to do it for me, just merely asking for advice. This is only part of the program that im stuck on. Just need a push in the right direction to see where i need to go, thats all.

    Thanks Phil, was having a look earlier and thats the kind of thing i was aiming towards, at least i now where i need to look in my text!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •