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:-