Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
2.67/5 (2 votes)
See more:
Dear All,

Regular expression for validating the password.
Requirement :
1) Minimum length --> 8 characters
2) Maximum length --> 50 characters
3) The first 8 characters must contain atleast 1-Lowercase, 1-Uppercase, 1-numeric, 1- Special characters.(Note : Allowed special characters are ~ ! * - _ @ # $ ^ & | > < .

Thank you and best regards in advance
Updated 12-Apr-13 17:08pm
Sergey Alexandrovich Kryukov 12-Apr-13 23:46pm    
Not very good requirements: don't limit maximum length (why?!), don't limit what you call "special characters" (they are not "special", who told you they are? :-).

What did you try so far?


try this..hope this helps you..

function validate() {
var dd = document.getElementById("txtbox");
var express = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,50}$/;
if (!express.test(dd.value)) {
alert("You must enter secure password");
Share this answer
Here is the advice: remove #2, remove limitation for "special characters", remove even worse requirement for "first 8". Those requirements only make the life of the owner of the password harder but only reduce its strength. (And just one warning in advance: I'm not going to listen to your argument the the requirements for the password are already given to you; in this case I think you should ask the person who gave your this spec, of let her/him ask use instead of you.)

Now, I would not use Regex. Simply right code (a Boolean predicate method accepting a string as input parameter) first checking the maximum length and then counting the characters of each class.

Regex should be used when it is really beneficial, not in all cases when string validation is needed. You can find out unlimited number of cases which cannot be validated using Regex in principle, or the solution would not be maintainable.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900