w3resource

C# Sharp Exercises: Validate a password

C# Sharp Regular Expression: Exercise-4 with Solution

Write a C# Sharp program to validate a password of length 7 to 16 characters with the following guidelines:
• Length between 7 and 16 characters.
• At least one lowercase letter (a-z).
• At least one uppercase letter (A-Z).
• At least one digit (0-9).
• Supported special characters: ! @ # $ % ^ & * ( ) + = _ - { } [ ] : ; " ' ? < > , .

Sample Data:
("[email protected]") -> True
("W#[email protected]") -> False
("[email protected]") -> False
("sdsd723#$Amid") -> True
("sdsd723#$Amidkiouy") -> False

Sample Solution:

C# Sharp Code:

using System;
using System.Text.RegularExpressions;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "[email protected]";
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Check the said string is a valid passward? " + test(text));
            text = "W#[email protected]";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string is a valid passward? " + test(text));
            text = "[email protected]";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string is a valid passward? " + test(text));
            text = "sdsd723#$Amid";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string is a valid passward? " + test(text));
            text = "sdsd723#$Amidkiouy";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Check the said string is a valid passward? " + test(text));
        }

        public static bool test(string text)
        {
            bool result = text.Length >= 7 && text.Length <= 16
            && Regex.IsMatch(text, "[A-Z]")
            && Regex.IsMatch(text, "[a-z]")
            && Regex.IsMatch(text, @"\d")
            && Regex.IsMatch(text, @"[!-/:[email protected]\[-_{-~]")
            && !Regex.IsMatch(text, @"[^\dA-Za-z!-/:[email protected]\[-_{-~]");
            return result;
        }
    }
}

Sample Output:

Original string: [email protected]
Check the said string is a valid passward? True

Original string: W#[email protected]
Check the said string is a valid passward? False

Original string: [email protected]
Check the said string is a valid passward? False

Original string: sdsd723#$Amid
Check the said string is a valid passward? True

Original string: sdsd723#$Amidkiouy
Check the said string is a valid passward? False

Flowchart:

Flowchart: C# Sharp Exercises - Validate a password

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous C# Sharp Exercise: Possible Palindromes from string of characters.
Next C# Sharp Exercise: Check two strings contain the same character pattern.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter