954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Secure password help!

Hi

Am writing some code for which takes down new members information and creates them a username etc. It's all gone well apart from one bit of coding that i am having trouble with.

When a user types their chosen password in, say....STEVEN.....then on the summary page at the end i want it to show S****N as the password.

My code at the moment:

secpass = (password.charAt(0) + password.charAt(password.length-1));

Can anyone make any suggestions as to what to add to this so that the letters in the middle of the password are changed to '*'

It's easy to add * inbetween the first and last letter but just cant figure out how to make the amount of * the same as the amount of middle letters.

Any help appreciated.

n33712

n33712
Newbie Poster
11 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

Something like the below should do the trick:

function fuzzy(pwd) {
    if(!pwd || pwd.constructor == String || pwd.length < 3)
        return(pwd);
    var arr = pwd.split("");
    return(arr[0] + new Array(arr.length - 1).join('*') + arr[arr.length - 1]);
}
/* OR */
function fuzzy(pwd) {
    if(!pwd || pwd.constructor == String || pwd.length < 3)
        return(pwd);
    return(pwd.replace(/(.)(.*)(.)/, function(a, b, c, d) { 
           return(b+ c.replace(/./g, "*") + d); }));
}
~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Hi

Thanks very much for the help. While i am sure this works and i can just about understand what it's doing (beginner) is there a simpler way of doing this?

Cheers

p.s. is it possible using count?

n33712
Newbie Poster
11 posts since Jan 2008
Reputation Points: 10
Solved Threads: 0
 

The first method I posted is pretty simple, it just uses some basic array and string operations. Read the javascript String and Array object documentation and get back if you have any more queries.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You