Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

password validation check for letters and numbers 1

Status
Not open for further replies.

mat41

Programmer
Mar 7, 2004
91
AU
This function seems to work except for the checking for a string that contains letters and at least one number. please help?

function ValidatePW() {
var error = "";
var illegalChars = /[\W_]/; // allow only letters and numbers
var fld = document.updateForm.tUserPassword

if (fld.value == "") {
fld.style.background = 'Yellow';
alert("You didn't enter a password.\n");
} else if ((fld.value.length < 8) || (fld.value.length > 15)) {
alert("The password must be a minimum of eight characters.");
fld.style.background = 'Yellow';
} else if (illegalChars.test(fld.value)) {
alert("The password contains illegal characters, enter letters and numbers only.");
fld.style.background = 'Yellow';
} else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
alert("The password must contain at least one number.");
fld.style.background = 'Yellow';
} else {
fld.style.background = 'White';
}
}
 
Hi

That is strange. Why punctuations, operators and special characters are rejected ? Why "p@$$w0rd" can not be a valid password ? People used to add some l33t characters to make their passwords secure.

Your regular expressions in the [tt]search()[/tt] parameters are probably not what you want : parenthesis ( () ) are for grouping, brackets ( [] ) are for character sets. Change them.


Feherke.
 
Thankyou for your time. For others I ended up going with:

if(document.updateForm.tUserPassword.value.length < 8)
{
alert("Password must contain at least eight characters. NOTE Passwords must be greater than eight characters, contain a number and upper and lower case letters.");
document.updateForm.tUserPassword.focus();
return false;
}
re = /[0-9]/;
if(!re.test(document.updateForm.tUserPassword.value))
{
alert("Password must contain at least one number (0-9). NOTE Passwords must be greater than eight characters, contain a number and upper and lower case letters.");
document.updateForm.tUserPassword.focus();
return false;
}
re = /[a-z]/;
if(!re.test(document.updateForm.tUserPassword.value))
{
alert("Password must contain at least one lowercase letter (a-z). NOTE Passwords must be greater than eight characters, contain a number and upper and lower case letters.");
document.updateForm.tUserPassword.focus();
return false;
}
re = /[A-Z]/;
if(!re.test(document.updateForm.tUserPassword.value))
{
alert("Password must contain at least one uppercase letter (A-Z). NOTE Passwords must be greater than eight characters, contain a number and upper and lower case letters.");
document.updateForm.tUserPassword.focus();
return false;
}
if(document.updateForm.tUserPassword.value != document.updateForm.checkTUserPassword.value)
{
alert("Your passwords do not match, confirm your password.");
document.updateForm.checkTUserPassword.focus();
document.updateForm.checkTUserPassword.select();
return(false);
}
 
Hi

Now that you unified the error messages I would unify abit the validations too :
JavaScript:
[b]var[/b] check[teal]=[/teal][teal]{[/teal]
  [green][i]'.{8,}'[/i][/green][teal]:[/teal][green][i]'eight characters'[/i][/green][teal],[/teal]
  [green][i]'[0-9]'[/i][/green][teal]:[/teal][green][i]'one number (0-9)'[/i][/green][teal],[/teal]
  [green][i]'[a-z]'[/i][/green][teal]:[/teal][green][i]'one lowercase letter (a-z)'[/i][/green][teal],[/teal]
  [green][i]'[A-Z]'[/i][/green][teal]:[/teal][green][i]'one uppercase letter (A-Z)'[/i][/green]
[teal]}[/teal]

[b]for[/b] [teal]([/teal][b]var[/b] exp [b]in[/b] check[teal])[/teal] [teal]{[/teal]
  [b]if[/b] [teal](![/teal]document[teal].[/teal]updateForm[teal].[/teal]tUserPassword[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][b]new[/b] [COLOR=darkgoldenrod]RegExp[/color][teal]([/teal]exp[teal])))[/teal] [teal]{[/teal]
    [COLOR=darkgoldenrod]alert[/color][teal]([/teal][green][i]'Password must contain at least '[/i][/green][teal]+[/teal]check[teal][[/teal]exp[teal]]+[/teal][green][i]'.[/i][/green][lime][i]\n[/i][/lime][green][i]NOTE Passwords must be greater than eight characters, contain a number and upper and lower case letters.'[/i][/green][teal]);[/teal]
    document[teal].[/teal]updateForm[teal].[/teal]tUserPassword[teal].[/teal][COLOR=darkgoldenrod]focus[/color][teal]();[/teal]
    [b]return[/b] [b]false[/b][teal];[/teal]
  [teal]}[/teal]
[teal]}[/teal]

[gray]// the tUserPassword vs checkTUserPassword check remains the same[/gray]
This approach make it easier to change if you want to accumulate all errors in a single message :
JavaScript:
[b]var[/b] check[teal]=[/teal][teal]{[/teal]
  [green][i]'.{8,}'[/i][/green][teal]:[/teal][green][i]'eight characters'[/i][/green][teal],[/teal]
  [green][i]'[0-9]'[/i][/green][teal]:[/teal][green][i]'one number (0-9)'[/i][/green][teal],[/teal]
  [green][i]'[a-z]'[/i][/green][teal]:[/teal][green][i]'one lowercase letter (a-z)'[/i][/green][teal],[/teal]
  [green][i]'[A-Z]'[/i][/green][teal]:[/teal][green][i]'one uppercase letter (A-Z)'[/i][/green]
[teal]}[/teal]

[b]var[/b] message[teal]=[/teal][green][i]''[/i][/green][teal];[/teal]
[b]for[/b] [teal]([/teal][b]var[/b] exp [b]in[/b] check[teal])[/teal]
  [b]if[/b] [teal](![/teal]document[teal].[/teal]forms[teal][[/teal][purple]0[/purple][teal]].[/teal]hmm[teal].[/teal]value[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][b]new[/b] [COLOR=darkgoldenrod]RegExp[/color][teal]([/teal]exp[teal])))[/teal]
    message[teal]+=[/teal][green][i]'Password must contain at least '[/i][/green][teal]+[/teal]check[teal][[/teal]exp[teal]]+[/teal][green][i]'.[/i][/green][lime][i]\n[/i][/lime][green][i]'[/i][/green]

[b]if[/b] [teal]([/teal]message[teal])[/teal] [teal]{[/teal]
  [COLOR=darkgoldenrod]alert[/color][teal]([/teal]message[teal]+[/teal][green][i]'NOTE Passwords must be greater than eight characters, contain a number and upper and lower case letters.'[/i][/green][teal]);[/teal]
  document[teal].[/teal]updateForm[teal].[/teal]tUserPassword[teal].[/teal][COLOR=darkgoldenrod]focus[/color][teal]();[/teal]
  [b]return[/b] [b]false[/b][teal];[/teal]
[teal]}[/teal]

[gray]// the tUserPassword vs checkTUserPassword check remains the same[/gray]

Feherke.
 
Feherke,

Very nice method! I've not seen it consolidated like that before. I'm putting this in my toolbox for later. Have a star on me.

Now, I do have a question. I'm still muddling through how the function works. I think my biggest stumbling block is this section:
Code:
  if (!document.forms[0].hmm.value.match(new RegExp(exp)))
    message+='Password must contain at least '+check[exp]+'.\n'

On first pass, the if function will parse thusly:
Code:
  if (!document.forms[0].hmm.value.match(new RegExp('.{8,}')))

What I don't get is how the message would pick up the next (exp). I would have thought the line would parse this way:
Code:
message+='Password must contain at least '+check['.{8,}']+'.\n'

What would cause the function to move to the next string in check? Or have I overlooked something?
 
Hi

LarrySteele said:
Or have I overlooked something?
I would say, yes. The is a [tt]for[/tt]..[tt]in[/tt] statement before the [tt]if[/tt] you quoted :
Feherke said:
Code:
[b]for[/b] [teal]([/teal][b]var[/b] exp [b]in[/b] check[teal])[/teal]
The [tt]for[/tt]..[tt]in[/tt] will iterate through the check object's properties, assigning ope property at a time to the exp variable.

Or I misunderstood your question...

Feherke.
 
Feherke,

You understood right. I guess what throws me is what would cause the for...in loop to advance to the next value.

Given the following var to check: "AbcDefGhi", here's how I see the flow going:

Pass one
Code:
for (var exp in check)
  if (!document.forms[0].hmm.value.match(new RegExp('.{8,}')))
    -- length is valid, so if is false, no message

Pass two
Code:
  if (!document.forms[0].hmm.value.match(new RegExp('eight characters')))
    -- var does not match this reg expression, so...
    message+='Password must contain at least '+check['eight characters']+'.\n'

Pass three would evaluate [tt][0-9][/tt] and so on...

I know the confusion is mine and it goes back to many, many years working with various flavors of BASIC. Flow control is different between the two languages. Got bit several times in my early days working with Java/JavaScript.

Thanks.

mat41 - don't mean to hijack the post, but wanted to get clarification on Feherke's sample. Looks like something I could use in the future, after I understand what makes it work the way it does.

- Larry



 
Wow what a piece of code, nothing short of brilliant. Thank you for your time and expertise.

Hijack I think not, all that happened was the great coe was explained.

thanks again
 
Hi

Larry said:
Pass two
Code:
  if (!document.forms[0].hmm.value.match(new RegExp('eight characters')))
    -- var does not match this reg expression, so...
    message+='Password must contain at least '+check['eight characters']+'.\n'
Got it. You misunderstood the syntax. Note there are colons ( : ) not commas ( , ) between the strings in each pair. There is a big difference between :
Code:
[b]var[/b] check[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Object[/color][teal]()[/teal]
check[teal][[/teal][green][i]'.{8,}'[/i][/green][teal]]=[/teal][green][i]'eight characters'[/i][/green][teal];[/teal]
check[teal][[/teal][green][i]'[0-9]'[/i][/green][teal]]=[/teal][green][i]'one number (0-9)'[/i][/green][teal];[/teal]
check[teal][[/teal][green][i]'[a-z]'[/i][/green][teal]]=[/teal][green][i]'one lowercase letter (a-z)'[/i][/green][teal];[/teal]
check[teal][[/teal][green][i]'[A-Z]'[/i][/green][teal]]=[/teal][green][i]'one uppercase letter (A-Z)'[/i][/green][teal];[/teal]
Code:
[b]var[/b] check[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]Array[/color][teal]()[/teal]
check[teal][[/teal][purple]0[/purple][teal]]=[/teal][green][i]'.{8,}'[/i][/green][teal];[/teal]
check[teal][[/teal][purple]1[/purple][teal]]=[/teal][green][i]'eight characters'[/i][/green][teal];[/teal]
check[teal][[/teal][purple]2[/purple][teal]]=[/teal][green][i]'[0-9]'[/i][/green][teal];[/teal]
check[teal][[/teal][purple]3[/purple][teal]]=[/teal][green][i]'one number (0-9)'[/i][/green][teal];[/teal]
check[teal][[/teal][purple]4[/purple][teal]]=[/teal][green][i]'[a-z]'[/i][/green][teal];[/teal]
check[teal][[/teal][purple]5[/purple][teal]]=[/teal][green][i]'one lowercase letter (a-z)'[/i][/green][teal];[/teal]
check[teal][[/teal][purple]6[/purple][teal]]=[/teal][green][i]'[A-Z]'[/i][/green][teal];[/teal]
check[teal][[/teal][purple]7[/purple][teal]]=[/teal][green][i]'one uppercase letter (A-Z)'[/i][/green][teal];[/teal]
Take a look at JSON for more about the syntax. ( You may find the JSON article on Wikipedia more concise. )

Feherke.
 
Feherke, thanks. You're right, I missed the colons. And never took the time to learn JSON before, so didn't recognize the syntax. Good stuff, thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top