You can get portions of the text from the field and turn 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 the bars are not part of the text... I don’t remember how this component behaves, but if there are bars, just take with a Replace
.
And if those conversions go wrong, it’s because they weren’t valid integers anyway.
Now just do two simple checks: whether the month is between 1 and 12, and whether the days are between 1 and the maximum of the month.
if (mes < 1 || mes > 12) {
throw new DataInvalidaMeuParsaException();
}
A tip to catch 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();
}
P.s.: merely illustrative exceptions. . NET has some more appropriate types of exceptions that already come with it, such as ArgumentException
or ArgumentOutOfRangeException
.
Thanks for the tip, I’m going through the code to see what I can implement, already gave a clear!
– Thiago Saracine
If the smart-ass Q gave down can show up and give the review, thank you... I imagine the reason kkkk
– Rovann Linhalis
@Rovannlinhalis alive taking of these :)
– Maniero
Only one hypothesis: it may have been because the above code allows invalid dates (February 31) and considers certain valid dates invalid (i.e.: 01/01/1890).
– Oralista de Sistemas
the part of the invalid dates, I quoted in the answer and just increment the checks, the part of valid dates as invalid, I believe it is at the discretion of the programmer, in the scenario that he is believe register a name information, Cpf, invalid email as of 01/01/1890 entry date, it is sufficient for it to define which years will be valid for the user to enter the @Renan record
– Rovann Linhalis
@Rovannlinhalis Solved yes, thank you!
– Thiago Saracine
@Rovannlinhalis marquei
– Thiago Saracine