By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,044 Members | 1,248 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,044 IT Pros & Developers. It's quick & easy.

password validation with reg exp

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ob****@ziplip.com (Obi-Too) writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.