Simple character validation.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
x = y.Lengthx is assigned to be the number of characters in string y. Can also be used to return the number of items in an array.
Char.IsUpper(c)Returns true if character c is upper case or false if not.
Char.IsLower(c)Returns true if character c is lower case or false if not.
Char.IsLetter(c)Returns true if character c is an alphabet letter a-z or A-Z.
Char.IsDigit(c)Returns true if character c is a digit 0-9.
Char.IsLetterOrDigit(c)Returns true if character c is an alphabet letter or a digit.
x = y.ToLower()x is assigned to be the lower case of y.
x = y.ToUpper()x is assigned to be the upper case of y (capital letters).
Questions to think about with this program to check your understanding:
Identify the Boolean variable used to flag if a character is not a number.
valid is a Boolean variable used to flag if a character is not a number.
State what data type is before the dot in Char.IsDigit(character).
“character” is a char data type, the C# type that holds a single character.
Change the program so that it:
validate_name to check whether the name only contains alphabet characters or spaces. Once a valid name is entered, it is output in upper case.Enter the name on the card:
Enter the 16 digit number:
JOHN SMITH
Card details valid.
Enter the name on the card:
Enter the 16 digit number:
JOHN SMITH
Card details valid.
validate_number
// Function to validate the number is numbers only
static bool validate_number(string number_on_card)
{
---
bool valid = true;
int number_of_characters = number_on_card.Length;
---
// Must be 16 (without spaces) or 19 characters (with spaces)
if (number_of_characters == 16 || number_of_characters == 19)
{
---
// Check each character in the number
foreach (char character in number_on_card)
{
---
// Anything other that a digit and space is invalid
if (!Char.IsDigit(character) && character != ' ')
{
---
valid = false;
---
}
---
}
---
}
---
else
{
---
valid = false;
---
}
---
return valid;
}
validate_name
// Function to validate the name is letters and space only
static bool validate_name(string name_on_card)
{
---
bool valid = true;
---
// Check each character in the name
foreach (char character in name_on_card)
{
---
// Anything other than a letter or a space is invalid
if (!Char.IsLetter(character) && character != ' ')
{
---
valid = false;
---
}
---
}
---
return valid;
}
input_card_details
// Input valid name and number for a debit card (not using check digits)
static void input_card_details()
{
---
bool valid_card = false;
string name_input = "";
---
// Only progress if the name is valid
while (!valid_card)
{
---
Console.WriteLine("Enter the name on the card:");
name_input = Console.ReadLine();
valid_card = validate_name(name_input);
---
}
---
valid_card = false;
string number_input = "";
---
// Only progress if the number on the card is valid
while (!valid_card)
{
---
Console.WriteLine("Enter the 16 digit number:");
number_input = Console.ReadLine();
valid_card = validate_number(number_input);
---
}
---
Console.WriteLine(name_input.ToUpper());
}