password validation with reg exp

O

Obi-Too

I need to validate a password to only allow A-Z, a-z and 0-9. I
thought this would work:

function Validate()
{
var passphrase = document.all.challenge.value;

var re = /[a-zA-Z0-9]/;
if (!re.test(passphrase))
{
alert("Please enter a valid password!");
return false;
}
else
{
return true;
}
}

however if the following is entered 045# - this is accepted a being
valid. What am i doing wrong?

thanks,

Obitoo
 
L

Lasse Reichstein Nielsen

I need to validate a password to only allow A-Z, a-z and 0-9. I
thought this would work: ....
var re = /[a-zA-Z0-9]/;

This RE matches any string that contain at least one digit or letter,
not just strings that contain only those.

Try
/^[a-z0-9]+$/i
This matches from the beginning of the string (^) to the end ($) and
between there are one or more letters or digits. The "i" means "ignore
case", so we don't have to write both a-z and A-Z.

Another test is
/[^a-z0-9]/i
this succeedes if the string contains any non-digit/letter. You can the
use
if (re.test(...)) { // password failed

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top