password validation (minimum # of characters and alphanumeric only)

Can you help. I have a password input text field ... (see below)

<input name="register_password" type="password" size="15" maxlength="20" onfocus="change2Msg(3);">

I need to build some validation into it ... to ONLY access alphanumeric characters A-Z a-z 0-9 with NO spaces nor other characters AND the password MUST be between 6 and 20 characters in length.

I'm don't want a pop up message ... I'm actually hoping to restrict the input of the password's unacceptable characters DURING input using something LIKE onkeyup='this.value=this.value.replace(blah);' where the blah is the stuff I don't understand and need your help with!  ...

SO to confirm ...
if I enter / click / tab into the password input field 'register_password' and I only input 4 characters ... I'm forced to stay in the input field until I enter at least 6 and no more than 20 ... AND those characters accepted are only alphanumeric.

Does this make sense? I know it's fussy but it's for a very specific need.

Who is Participating?
Sorry, in my code I allowed only numbers... the right regexp is /[^a-zA-Z0-9]/g or /[^0-9a-z]/gi (as Riyasjef suggested)!
I don't think it makes much sense... but here it is ;)

<input name="register_password" type="password" size="15" maxlength="20" onkeyup="this.value = this.value.replace(/[^0-9]/g, '');" onblur="if (this.value.length < 6) this.focus();">

I suggest you to alert all these errors instead of replacing the value or focusing the field... and to use a server-side validation too!

To allow only alphanumerics, this can be modified as

<input name="register_password" type="password" size="15" maxlength="20" onkeyup="this.value = this.value.replace(/[^0-9A-Z]/gi, '');" onblur="if (this.value.length < 6) this.focus();">

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.