How to reset User Password If he gorgets?
In this blog post, we will explore a simple JavaScript function that can quickly generate a random password with just a click of a button. This code can be particularly useful in generating passwords for your Google Apps Script protected web application users. In case a user forgets their password and wishes to log in, they can reset their password using this function. If the user provides their registered email, the system will validate it and a random password will be sent to their registered email. This password can be used to log in to their account.
The JavaScript code for generating random passwords is straightforward and easy to understand. Here's the code snippet:
Javascript code
function gPass(){
var uP = '';
var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'+'abcdefghijklmnopqrstuvwxyz0123456789@#$';
for (let i = 1; i <= 8; i++) {
var char = Math.floor(Math.random()* str.length + 1);
uP += str.charAt(char)}
document.getElementById('response').innerHTML = uP;
}
</script>
<button onclick="gPass()">New Password</button>
<br>New Password:
<div id="response"></div>
The "gPass()" function generates a random password consisting of eight characters by using a combination of uppercase and lowercase letters, digits, and special characters such as @#$.
The function initializes an empty string variable "uP" to store the generated password. It then creates a string "str" that contains all the characters that can be used to create the password.
The function uses a "for" loop that runs eight times to generate each character of the password. Inside the loop, a variable named "char" is generated by using the "Math.floor()" and "Math.random()" functions to pick a random number between one and the length of "str". The "charAt()" method is then used to select the character at the position of the generated random number from "str" and append it to the "uP" variable.
After the loop completes, the "innerHTML" property of the "response" element is set to the generated password "uP".
Finally, a button element with the "onclick" attribute is created with the label "New Password" that calls the "gPass()" function when clicked, and a div element with the "id" attribute "response" is created to display the generated password.
In conclusion, generating random passwords using JavaScript is an easy and efficient way to create strong passwords for your online accounts. By using this code, you can quickly create a new password for each account you have without having to remember it. However, it's essential to note that this method is not secure enough for storing passwords. Therefore, it's always recommended to use a password manager to store and manage your passwords securely.