DEV Community

Cover image for C# programm to print out validated products names like this format XX-00.
Muhammed Ismail
Muhammed Ismail

Posted on

C# programm to print out validated products names like this format XX-00.

Question:
Implement validation for the product names entered by the user. A valid product name should consist of letters, hyphens, and an integer between 200 and 500.

Examples of valid product names:

CE-400

XX-480

LABAN-231

Examples of invalid product names:

CE400

XX3-480

LABAN-100

Provide specific error messages based on the type of error the user makes.

Watch this video:
Solution:

using System.Text.RegularExpressions;

var regex = @"^[A-Za-z]+-[0-9]+$";

int index = 0;

string[] MyProds = new string[0];

while (true)

    try

    {

        Console.Write("Enter the product name like this format XX-200: ");

        var ProductName = Console.ReadLine();

        if (ProductName.ToLower().Trim() == "exit")

        {

            break;

        }

        if (!Regex.IsMatch(ProductName, regex))

        {

            if (!ProductName.Contains("-"))

            {

                Console.ForegroundColor = ConsoleColor.Red;

                Console.WriteLine("Wrong product name");

                Console.ResetColor();

                continue;

            }

            if (!Regex.IsMatch(ProductName.Split("-")[0], @"^[A-Za-z]+$"))

            {

                Console.ForegroundColor = ConsoleColor.Red;

                Console.WriteLine("Wrong left  side, it should be just alphabets");

                Console.ResetColor();

                continue;

            }

            if (!Regex.IsMatch(ProductName.Split("-")[1], @"^[0-9]+$"))

            {

                Console.ForegroundColor = ConsoleColor.Red;

                Console.WriteLine("Wrong right side, it should be just numbers");

                Console.ResetColor();

                continue;

            }

        }

        bool isTrueNumber = int.TryParse(ProductName.Split("-")[1], out int correctNumbers);

        if (isTrueNumber)

        {

            int parsedInt = int.Parse(ProductName.Split("-")[1]);

            if (parsedInt >= 200 && parsedInt <= 500)

            { }

            else

            {

                Console.ForegroundColor = ConsoleColor.Red;

                Console.WriteLine("Wrond product name it should be between 200 and 500");

                Console.ResetColor();

            }

        }

        Array.Resize(ref MyProds, MyProds.Length + 1);

        MyProds[index] = ProductName;

        index++;

    }

    catch(Exception ex)

    {

        Console.WriteLine("The expression is wrong form ");

    }

 }

Console.WriteLine("----------- The right product names without sorting --------------- \n\n");

foreach (var prod in MyProds)

    Console.WriteLine("{0} *- ", prod);

Array.Sort(MyProds);

Console.WriteLine("\n\n----------- The right product's sorted names --------------- ");

foreach (var prod in MyProds)

    Console.WriteLine("{0} *- ", prod);
```


`



I hope that was a useful and understandable exercise.  For more questions please write it in the comments box below and I will reply to you as soon as possible. 

Subscribe to my channel on [YouTube](https://www.youtube.com/@Devismail-u3s) to get more effective videos in C# programming. 

Read More on [My Blog](https://buymeacoffee.com/devismail) 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)