Hi,

I've looked online for a few days but haven't been able to find an adequate solution.

I'm just trying to use javascript to validate that a password doesn't contain user data (first name, last name, etc...)

I really thought it would be simple. Here is what I'm using:

function validate_fname(field,alerttxt)
{
with (field)
{
re = /form.firstName.value/;
if(re.test(value))
  {alert(alerttxt);return false}
else {return true}
}
}

function validate_form(thisform)
{
with (thisform)
{
if (validate_fname(password,'Password must not contain users first name')==false)
{password.focus();return false} 
}
}

All the other validation I have on the page works (length, special chars, lower & upper case) but I can't seem to get it to check if the password contains their first name.

I have seen many pages that mention this being a good practice, but not how to implement it. I have been stuck on this one for weeks and I'm just clueless what else to do.

Thanks for any help you can provide.
Danny

Try this:

var re = form.firstName.value;
if(re.indexOf(value) !== -1) 
 {alert(alerttxt);return false}
else {return true}

Hi, thanks for your response.

I tried using exactly what you posted, but it didn't seem to work. I tried putting the users first name in the password (6Clark$6) but it allowed it.

Any other suggestions?

Thanks again.

What happens when you alert(form.firstName.value); ?

I get a pop up message saying "Clark".

I also played around with changing the -1 value to 1, but it didn't allow any passwords to go through, regardless of them containing the word Clark or not, but that seems to be a property of the indexOf so I suppose it's working as it should.

I'm really at a loss, and afraid I'm just missing some minor detail or perhaps making this harder than I should be?

I'm sorry I wrote it in backwards should be this:

if(value.indexOf(re) !== -1)

Thank you, Thank you, Thank you, THANK YOU!!!!

That did it! It works!

Thank you again! I truly appreciate your help!

One last thing....This isn't a requirement for my project, but I am curious...

If I wanted to make sure they didn't include any user data, regardless of it being in lowercase or uppercase, should I convert form.fname.value to uppercase then test it, and then convert it to lowercase and test it?

I would convert both the password and userdata to lower case than test it.

Perfect. Thanks again!

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.