You can get the text portions of the field and transform them into numbers:
string textoBruto = this.TextBoxN.Text;
int dia = Convert.ToInt32(textoBruto.Substring(0, 2), 10); // onde 10 significa base decimal
int mes = Convert.ToInt32(textoBruto.Substring(2, 2), 10);
int ano = Convert.ToInt32(textoBruto.Substring(4, 4), 10);
I suppose that the bars are not part of the text ... I do not remember how this component behaves, but if there are bars, just take with a Replace
.
And if these conversions fail, it's because they were not valid integers anyway.
Now just do two simple checks: if the month is between 1 and 12, and if the days are between 1 and the maximum of the month.
if (mes < 1 || mes > 12) {
throw new DataInvalidaMeuParsaException();
}
A hint for getting the biggest possible day of the month: you can create a hashmap, or cheat with an Array:
int[] maioresDias = new int[] {
31,
ano % 4 == 0 ? 29 : 28, // verificação de bissexto
31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
And now:
if (dia < 1 || dia > maioresDias[mes - 1]) {
throw new ParaCeTaLokException();
}
Ex .: merely illustrative exceptions. .NET has some more appropriate exception types that already come with it, such as ArgumentException
or ArgumentOutOfRangeException
.