In C# Create a console project. Have users to enter name and credit card number,
ID: 3865571 • Letter: I
Question
In C# Create a console project.
Have users to enter name and credit card number, verify the information.
1. Name field:
-- both first name and last name must be entered separated by a single space.
-- Only letters can be accepted.
-- Use patten "[a-zA-Z]{2,15}s[a-zA-Z]{2,15}" or ^[a-zA-Z]+ [a-zA-Z]$+
2. Card Number field:
-- Only digits can be accepted.
-- Pattern: "^[0-9]{12,19}$"
Reference:
https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/az24scfc(v=vs.110).aspx
Explanation / Answer
using System.IO;
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
//Declarations
string userName, creditCardNo;
//Taking user's name input
Console.WriteLine("Enter User name");
userName = Console.ReadLine();
//Taking user's credit card number input
Console.WriteLine("Enter User's Credit Card Number");
creditCardNo = Console.ReadLine();
string userNamePattern, creditCardNoPattern;
//Username pattern
userNamePattern = @"[a-zA-Z]{2,15}s[a-zA-Z]{2,15}";
//Declaring username regex
Regex userNameregex = new Regex(userNamePattern);
//Checking for the username with regex
bool checkUsername = userNameregex.IsMatch(userName);
//Credit card number pattern
creditCardNoPattern = @"^[0-9]{12,19}$";
//Declaring Credit card number regex
Regex creditCardregex = new Regex(creditCardNoPattern);
//Checking for the Credit card number with regex
bool checkCreditCardNo = creditCardregex.IsMatch(creditCardNo);
//Printing the output
if(checkUsername == false && checkCreditCardNo == true)
Console.WriteLine("User's name is wrong");
else if (checkUsername == true && checkCreditCardNo == false)
Console.WriteLine("User's Credit Card Number is wrong");
else if(checkUsername == false && checkCreditCardNo == false)
Console.WriteLine("Both User's name and credit Card Number are wrong");
else
Console.WriteLine("Both User's name and credit Card Number are correct");
}
}