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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex to validate passwords 3

Status
Not open for further replies.

serpento

Programmer
Jun 16, 2002
92
GB
I'm not too good at regular expressions yet, so I would much apprieciate it if someone could tell me one that returns true if a password only just uses characters a-z, A-Z and 0-9 and returns false if it uses any other character. -Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
you have the answer rate there already
a-zA-Z0-9
so
patrn = /([a-zA-Z0-9])/

function testIT(str) {
patrn = /([a-zA-Z0-9])/;
if (!(patrn.test(str))){
alert("bad");
return false; }
else {
alert("good");
return true;
}
})) _______________________________________________
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
should make that global also
/([a-zA-Z0-9])g/ _______________________________________________
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
don't know where that came from }))
see what happens when you start writing twenty functions doing different things in one page to test things. [smile] _______________________________________________
{ str = "sleep is good for you. sleep gives you the energy you need to function";
ptr = /sleep/gi;Nstr = str.replace(ptr,"coffee");alert(Nstr); }

_______________________________________________
for the best results to your questions: FAQ333-2924

 
It may be better to test that those characters are not in the string, as it is the string "abcdefgh-" would return true because it does contain matches to the pattern we gave.

If instead you said this:
Code:
patrn = /([^a-zA-Z0-9])/

Then if it is true you know there is an invalid character in the pattern, otherwise all the characters must belong to the set we specified (ie, the regex returns false)


Quick examp[le for proof (ok, I admit it, I had to test it in casemy memory failed me :p )
Code:
<html>
<head>
<script language=&quot;JavaScript&quot;>
<!--
function testText(){
    var str = &quot;abcdefgh123_&quot;;
	var str2= &quot;abcdefgh123&quot;;

	var patrn = /([a-zA-Z0-9])g/;
	alert(&quot;Reg Exp: [a-zA-Z0-9] is &quot; + patrn.test(str) + &quot; for string: &quot; + str + &quot;\n and is &quot; + patrn.test(str2) + &quot; for string: &quot; + str2);

	patrn = /([^a-zA-Z0-9])/;
	alert(&quot;Reg Exp: [^a-zA-Z0-9] is &quot; + patrn.test(str) + &quot; for string: &quot; + str + &quot;\n and is &quot; + patrn.test(str2) + &quot; for string: &quot; + str2);

}
//-->
</script>
</head>
<body>
<form name=&quot;myForm&quot;>
<input type=&quot;button&quot; name=&quot;b1&quot; onCLick=&quot;testText();&quot; value=&quot;run js&quot;>
</body>
</html>

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
I thought the regex you'd want would be more like

/^[a-zA-Z0-9]+$/

the ^ and the $ anchoring the regex to the beginning and end of the line.

the + saying there can be 1 or more occurence of the characters in the set.

(The anchor characters are from python, perl and php, I'm hoping they're the same in javascript)

-Rob
 
If you like specific lengths by the by you can do

Code:
/^[a-zA-Z0-9]{5-10}$/

to only return true against strings which have those characters repeated 5 to 10 times (5-10 is within curly braces)


-Rob
 
Thanks all of you. -Ed ;-)

________________________________
Destiny is not a matter of chance; it is a matter of choice.
 
skiflyer: very nice, didn't think of that one :p
That goes one step dfurther by allowing you to validate size as well as content in a single pattern, I like it. (ctrl+c, ctrl+v) :)

Thanks serpento, we're all glad to be of assistance...

-Tarwn ________________________________________________________________________________
Want to get great answers to your Tek-Tips questions? Have a look at faq333-2924
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top