Question

In: Computer Science

Write a PHP program that checks the elements of a string array named $Passwords. Use regular...

Write a PHP program that checks the elements of a string array named $Passwords. Use regular expressions to test whether each element is a strong password.

For this exercise, a strong password must have at least one number, one lowercase letter, one uppercase letter, no spaces, and at least one character that is not a letter or number. (Hint: Use the [^0-9A-Za-z] character class.) The string should also be between 8 and 16 characters long.

The $Passwords array should contain at least 10 elements, and at least six of the elements should fail. Ensure that one entry fails each of the five regular expression tests and that at least one fails because of the length. Display whether each password in the $Passwords array was strong enough, and display the test or tests that a password failed if it is not strong enough. Save the script as PasswordStrength.php.

Solutions

Expert Solution

Code:

<?php
$password = array("alex@God","ALEX@GOD1","alex@god1","alex @god1","alexGod1","alex@G1","alex@GOD1","ALEXgod@123","ALEXgod@2","alex@GOD1");
for ($x = 0; $x <10; $x++) {
$pass = $password[$x];
echo $pass. " ";
echo number($pass);
echo lower($pass);
echo upper($pass);
echo noword($pass);
echo space($pass);
echo len($pass);
echo "\n";
}
function number($pass){
if (!preg_match("/^(?=(.*[\d]){1,})/",$pass)) {
return "Atleast 1 digit required.";
}
return;
}
function lower($pass){
if (!preg_match("/^(?=(.*[a-z]){1,})/",$pass)) {
return "Atleast 1 lower case required.";
}
return;
}
function upper($pass){
if (!preg_match("/^(?=(.*[A-Z]){1,})/",$pass)) {
return "Atleast 1 upper case required.";
}
return;
}
function noword($pass){
if (!preg_match("/^(?=(.*[\W]){1,})/",$pass)) {
return "Atleast 1 special character required.";
}
return;
}
function space($pass){
if (!preg_match("/^(?!.*\s)/",$pass)) {
return "No space allowed.";
}
return;
}
function len($pass){
if(strlen($pass)>16){
return "The length should be less than 16.";
}
if(strlen($pass)<8){
return "The length should be greater than or equal to 8.";
}
return;
}

?>
Screenshot:

Code:

OUTPUT:

Explanation of Code:

  1. $password = array("alex@God","ALEX@GOD1","alex@god1","alex @god1","alexGod1","alex@G1","alex@GOD1","ALEXgod@123","ALEXgod@2","alex@GOD1");
    -> The array named password is created which have 10 test cases in this.
  2. for ($x = 0; $x <10; $x++) {
    $pass = $password[$x];
    echo $pass. " ";
    echo number($pass);
    echo lower($pass);
    echo upper($pass);
    echo noword($pass);
    echo space($pass);
    echo len($pass);
    echo "\n";
    }

    -> We are running a for loop on the array. Printing the test case. And also print the retuned value from the functions number, lower. upper, noword, space and len.
  3. function number($pass){
    if (!preg_match("/^(?=(.*[\d]){1,})/",$pass)) {
    return "Atleast 1 digit required.";
    }
    return;
    }

    -> Here we are matching the regex pattern with the test case. And returning the error is there is one. And same is done for all the function.
  4. Now we see the regex.
    "." (only dot) means Matches any character expect.
    "*" (only *) means match from 0 to unlimited times.
    [/d] matches digit
    [a-z] matches alphabet from a to z.
    [A-Z] matches alphabet from A to Z.
    [\W] match for special character.
    \s check for space.
    {1,} should be their at least ones.

Note: For any change in the code or doubt please feel free to comment. Thanks.


Related Solutions

Write a PHP Program to display all the $_SERVER elements
Write a PHP Program to display all the $_SERVER elements
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements....
Write a c program arrays2.c that checks if an integer array contains three identical consecutive elements. Assume the number of identical consecutive elements are no more than three if they exist. Sample input/output #1: Enter the length of the array: 11 Enter the elements of the array: -12 0 4 2 2 2 36 7 7 7 43 Output: The array contains 2 of three identical consecutive elements: 2 7 Sample input/output #2: Enter the length of the array: 4...
Program in C: Write a program in C that reorders the elements in an array in...
Program in C: Write a program in C that reorders the elements in an array in ascending order from least to greatest. The array is {1,4,3,2,6,5,9,8,7,10}. You must use a swap function and a main function in the code. (Hint: Use void swap and swap)
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements....
ASSIGNMENT: Write a program to reverse an array and then find the average of array elements. Start by creating 2 arrays that can each hold 10 integer values. Then, get values from the user and populate the 1st array. Next, populate the 2nd array with the values from the 1st array in reverse order. Then, average the corresponding elements in the 1st and 2nd arrays to populate a 3rd array (average the 1st element of the 1st array with the...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
How to write a C++ program that lets the user enter a string and checks if...
How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.
Write an ARMv8 program to sort an array of elements. As Imentioned in class, this...
Write an ARMv8 program to sort an array of elements. As I mentioned in class, this problem uses a portion of your Programming Assignment 1 where you computed the smallest and largest values in an array. Here we will extend that assignment to find the “index” of the smallest and the index of the largest elements of the array. The following C code segment illustrates how we can sort the element of an array.For this problem, assume an array with...
write a mips program to find the number of elements of the array that are divisible...
write a mips program to find the number of elements of the array that are divisible by 3.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT