Click here to Skip to main content
15,878,970 members
Articles / Programming Languages / Javascript

Password pattern check using JavaScript

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
7 Aug 2007CPOL1 min read 42.6K   7   4
Password validation using JavaScript.

Introduction

If we want to force a password to be a combination of alphabets and numbers and force a range of chars, we can simply do it in JavaScript.

Background

Simply use JavaScript to make sure that the user enters a combination of alphanumeric data and check for a minimum number of chars and maximum allowed chars. This code checks for at least one alphabetic char and one numeric char.

Using the code

JavaScript
function validatePassword(fieldName,minNumberOfDigits, maxNumberOfDigits) {
 var alphaNumericPattern =  "^[a-z0-9/_/$]{" + 
     minNumberOfDigits + "," + maxNumberOfDigits + "}";
 var regExpr = new RegExp(alphaNumericPattern,"i");
 var sourceField = event != null ? event.srcElement:e.target;
 if(fieldName != null && fieldName != "null" && fieldName != "undefined") {
   sourceField = document.getElementById(fieldName);
 }
 var message = "Password must be a combination of alphabets and numbers";
 message = message + "\n and must be between " + minNumberOfDigits + 
           " and " + maxNumberOfDigits + " chars.";
 var sourceFieldValue = sourceField.value;
 if(sourceFieldValue.length < minNumberOfDigits || 
            sourceFieldValue.length > maxNumberOfDigits){
  alert(message);
  sourceField.focus();
  return false;
 }
 if (!regExpr.test(sourceFieldValue)) {
  alert(message);
  sourceField.focus();
  return false;
 }
   regExpr = new RegExp("[a-z/_/$]{1}","i");
   if(!regExpr.test(sourceFieldValue)){
    alert(message);
    sourceField.focus();
    return false;
   }
   regExpr = new RegExp("[0-9]{1}","i");
   if(!regExpr.test(sourceFieldValue)){
     alert(message);
     sourceField.focus();
     return false;
   }
}
var alphaNumericPattern =  "^[a-z0-9/_/$]{" + minNumberOfDigits + 
                           "," + maxNumberOfDigits + "}";

It checks for any one of the following: a-z or 0-9 or _ or $. If the user enters anything other than the supported chars, it will alert the user.

At the same time, it makes sure that it meets the minimum number of chars and the maximum allowed chars.

JavaScript
regExpr = new RegExp("[a-z/_/$]{1}","i");

It checks for at least one alphabetic char:

JavaScript
regExpr = new RegExp("[0-9]{1}","i");

and it checks for atl east one number.

If it fails any one of the checks, it will alert the user and set the focus back on to the source field.

So you would assign this function on the onchage or onblur event.

HTML
<input type="password" onblur="return validatePassword(,6,12)">

or:

HTML
<input type="password" onchange="return validatePassword('null',6,12)">

Note: It is always advisable to double check the data before you insert or store it somewhere. You need to use server side validation to make the data foolproof. Do not just rely on JavaScript validations.

License

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


Written By
Web Developer
United States United States
Fourteen years of progressive experience in Software Product Development, tactical planning, project and client management, demonstrated success in leadership, critical thinking, problem solving and analysis. Diverse knowledge and experience with multiple development methodologies, reengineering, software engineering, and integration of heterogeneous systems.

Comments and Discussions

 
GeneralProblems... Pin
aprenot8-Aug-07 5:08
aprenot8-Aug-07 5:08 
GeneralRe: Problems... Pin
senthil karuppaiah8-Aug-07 8:09
senthil karuppaiah8-Aug-07 8:09 
GeneralRe: Problems... Pin
aprenot8-Aug-07 8:16
aprenot8-Aug-07 8:16 
GeneralRe: Problems... Pin
senthil karuppaiah8-Aug-07 8:27
senthil karuppaiah8-Aug-07 8:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.