Question
I'm creating a password generator of n length. The user deciedes the length by inputting a number. I'm having trouble with how to validate that
I'm creating a password generator of n length. The user deciedes the length by inputting a number. I'm having trouble with how to validate that 'num' is between the values of 6-25 also If the number entered by the user is valid, I want to use a for loop that iterates that number of times. In each iteration of the loop, randomly select one of the characters from the chars string and concatenate it to the password variable.
Here is my HTML
Generate a Strong Password
JAVASCRIPT/ JQUERY
"use strict";
$(document).ready(function() {
var getRandomNumber = function(max) {
var random;
if (!isNaN(max)) {
random = Math.random(); //value >= 0.0 and < 1.0
random = Math.floor(random * max); //value is an integer between 0 and max - 1
random = random + 1; //value is an integer between 1 and max
}
return random;
};
$("#generate").click(function() {
var isValid = true;
var num = $("#num").val().trim();
//HELP
if(num.length < 6 & num.length > 25){
$("#num").next().text("Please enter a valid number 6 - 25");
isValid = false;
}else{
$("#num").next().text("");
}
$("#num").val(num);
if(isValid == "true"){
for(var i = 1; i <= num; i ++){
r = getRandomNumber().random;
}
$("#password").val(r);
}
//HELP
$("#password").val( "" ); // clear previous entry
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-+!@";
}); // end click()
$("#clear").click(function() {
$("#num").val( "" );
$("#password").val( "" );
$("#num").focus();
}); // end click()
// set focus on initial load
$("#num").focus();
}); // end ready()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started