• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1389
  • Last Modified:

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.

  • 2
3 Solutions
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();">

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)!

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now