Password Strength Regular Expression

P

pramodx

Hi

I just need a regular expression for the condition that the password
has to be a combination of alphabets and numbers. It cannot be only
numbers nor can it be only alphabets. No special characters allowed.

Can someone please help me out with a regex for this?

Any advice would be grateful

Thanks,
 
E

Evertjan.

pramodx wrote on 12 dec 2008 in comp.lang.javascript:
I just need a regular expression for the condition that the password
has to be a combination of alphabets and numbers. It cannot be only
numbers nor can it be only alphabets. No special characters allowed.

Can someone please help me out with a regex for this?

<script type='text/javascript'>

var pw='a2'

alert(/[a-z]/i.test(pw) && /\d/.test(pw) && !/[^a-z\d]/i.test(pw))

</script>
 
L

Lasse Reichstein Nielsen

pramodx said:
I just need a regular expression for the condition that the password
has to be a combination of alphabets and numbers. It cannot be only
numbers nor can it be only alphabets. No special characters allowed.

Can someone please help me out with a regex for this?

As Evertjan suggested, you should probably use more than one regexp to
check the different requirements, instead of encoding all the
requirements in one.

If that's not possible, a more complex (and therefore less readable
and less maintainable) single regexp could be
/^(?:\d+[a-z]|[a-z]+\d)[a-z\d]*$/i

(I notice you didn't have any requirements on the length of the
password. If you add those, the single regexp becomes significantly
more complex, but it's only one simple test to add outside the regexp)

/L
 
P

pramodx

Thanks Evert. It worked

pramodx wrote on 12 dec 2008 in comp.lang.javascript:
I just need a regular expression for the condition that thepassword
has to be a combination of alphabets and numbers. It cannot be only
numbers nor can it be only alphabets. No special characters allowed.
Can someone please help me out with a regex for this?

<script type='text/javascript'>

var pw='a2'

alert(/[a-z]/i.test(pw) && /\d/.test(pw) && !/[^a-z\d]/i.test(pw))

</script>
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top