In: Computer Science
Using JavaScript
You must submit a file called, called pass.js
pass.js: Validator of passwords
Your task this week will be to program a valid password validator.
A password is safe only if it satisfies the following constraints:
- It has from 5 to 10 characters (inclusive)
- It contains at least one lowercase letter, at least one capital
letter and at least one number
- The first character is different from the last
Once the password is entered and validated, the user is asked to copy it again to validate.
Course of the program
More precisely :
1. Ask the user to enter a password with:
prompt ("Please choose a password");
2. Check if the password matches the requirements
2a. If the password is invalid, display "Invalid password" and end the program (the rest of the program runs only if this step was done correctly)
2b. If the password is correct, go to step 3
3. Ask the user to enter the password again with:
prompt ("Please enter the password a second time to confirm");
4. Check if both passwords are the same
4a. If the passwords do not match, show "You
did not enter the same password twice" and ask to start again
4b. If the two entered passwords match, go to
step 5
5. Once the password is valid and entered twice correctly, display "Password saved!"
Here is an example of the execution of the program :
Please choose a password
> the user enters "abc"
Invalid password
Another example :
Please choose a password
> the user enters "l33th4xx0r"
Please enter the password a second time to
confirm
> the user enters "abc"
You have not entered the same password twice
Please enter the password a second time to confirm
> the user enters "l33th4xx0r"
Password saved!
Your program must be complete, with comments that
indicate the name of the file, the author (your name), a
brief
description of the usefulness of the program. There must also
be
comments that explain what each variable corresponds to,
and the operation of the program. Use statements of
appropriate loop, correct indentation, and block statements
in the body of if and loops. Your program should avoid
redundant and repetitive calculations.
/*
Filename : pass.js
Author : (Your Name)
This program validates password against,
- It has from 5 to 10 characters (inclusive).
- It contains at least one lowercase letter, at least one capital
letter and at least one number.
- The first character is different from the last.
*/
// variable to take input password from user
var passsword= prompt("Please choose a password");
// Regular expression to check wheather the password has 1 digit,1
uppercase character, 1 lowercase character and
//having 5-10 character length both inclusive.
var regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{5,10}$/;
//Condition to check the regular expression and first and last
digit of password is different
if (passsword.match(regex) && passsword.charAt(0) !=
passsword.charAt(passsword.length-1)){
var confirmpass = prompt ("Please enter the password a
second time to confirm");
//If both entry for password is equal
if(passsword == confirmpass){
alert("Password saved!");
}else{
alert("You did not enter the same
password twice");
}
}else{
alert("Invalid password");
}