I want to check if a string is of type ABC123.
- The length of the string must be 6.
- Only letters and numbers are allowed.
- The first three values of the string must be letters.
This is what I have done. How can I improve the code?
private void ValidationOfFlightCode(string flightCode)
{
if (flightCode.Length == 6)
{
bool state = Regex.IsMatch(flightCode, @"^[a-zA-Z0-9]+$");
if (state)
{
bool isLetter = false;
for (int i = 0; i < 3; i++)
{
isLetter = Char.IsLetter(flightCode, i);
if (!isLetter)
break;
}
if (isLetter)
{
MessageBox.Show(flightCode + ": " + state);
}
else
{
MessageBox.Show("The letters must be before the numbers");
}
}
else
{
MessageBox.Show("Only letters and numbers are allowed!");
}
}
else
{
MessageBox.Show("Flight Code must be 6 characters long");
}
}