Menu
blogs-logo
  • Home
  • About Us
  • Courses
    • Angular JS
    • Angular
    • Vue JS
    • Full Stack
    • Ionic Framework
    • Mean Stack
    • Mongo DB
    • Node JS
  • Course Schedule
  • Blog
  • Videos
  • Contact
blogs-logo
How-To-Create-Strong-Password-In-Javascript-With-Number-Alphabet-And-Symbols

How To Create Strong Password In JavaScript With Number, Alphabet And Symbols.

Posted on January 3, 2022May 13, 2022

Step1:- Add textbox to display Password

< input type=”text” id=”password” >

Step2:- Declare variable and regular expression pattern

var allChars = “0123456789abcdefghijklmnopqrstuvwxyz!@#$%^&*()ABCDEFGHIJKLMNOPQRSTUVWXYZ”;
var pwd= “”;
var pattern = new RegExp(“^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d)(?=.*[-+_!@#$%^&*.,?]).+$”);

Step3:- Create generatePassword function and check password validation

function generatePassword() {
for (var i = 0; i <= 8; i++) {
var randomNumber = Math.floor(Math.random() * allChars.length);
pwd += allChars.substring(randomNumber, randomNumber +1);
}
if(pattern.test(pwd)){
document.getElementById(“password”).value = pwd;
} else {
pwd=””;
generatePassword();
}
}

generatePassword();

Step4:- Add button to copy strong password

< button onclick=”copyPassword()” >Copy< /button>

Step5:- Create function to copy generated password

function copyPassword(){
document.getElementById(“password”).select();
document.execCommand(“copy”);
}

Example:-

Recent Posts

  • Understand What Is MEAN Stack?
  • How To Create Strong Password In JavaScript With Number, Alphabet And Symbols.
  • How to disable right-click in PDF file in browser

Archives

  • May 2022
  • January 2022
  • September 2021

Categories

  • JavaScript
  • Uncategorized
©2025 MeanJS Training Institute | Powered by SuperbThemes & WordPress