Solved

password validation (minimum # of characters and alphanumeric only)

Posted on 2004-10-14
3
1,330 Views
Last Modified: 2008-01-09
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.

Richard
0
Comment
Question by:rcbuchanan
  • 2
3 Comments
 
LVL 3

Assisted Solution

by:Kupi
Kupi earned 300 total points
ID: 12310810
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!

Kupi
0
 
LVL 9

Assisted Solution

by:riyasjef
riyasjef earned 200 total points
ID: 12311376
Hi
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();">

Riyasjef
0
 
LVL 3

Accepted Solution

by:
Kupi earned 300 total points
ID: 12311485
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)!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now