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

Recommended Answers

All 3 Replies

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); }));
}

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?

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.