Validation of Data Regex

Asked

Viewed 331 times

-1

Good Morning ...

I would like to know how I can validate a date ! Where the Final Date Cannot be less than the Initial Date (dataFinal < stardate) with regex !

1 answer

1


Regex, definitely, is not the right tool to solve this problem.

If you want to compare two dates, then use the struct DateTime.

I’ll assume the input is two strings, s1 and s2, in a valid format.

var d1 = DateTime.Parse(s1)
var d2 = DateTime.Parse(s2)

d1 < d2

If you are not sure that the format of the strings is valid, then you should use DateTime.TryParse

  • Well I already managed to transform from String to Datetime like this: Datetime dateInitil = Convert.Todatetime(dtIni.Text); The question is how to not let the code continue if the FINAL date is less than the Initial

  • @Lucianoramos As I explained, just use the operator < to compare two instances of DateTime.

  • Well I understood , however Cód continues and brings a table of records, but I do not want to take into account that the date is incorrect !

  • 1

    @Lucianoramos In this case you have a bug in the code. You have to put a new question (since this has already been answered), and to provide a MCVE so we can help you.

  • Um got it, thank you very much for the help ;-)

  • @We pray for nothing, and welcome to Stackoverflow! Don’t forget to accept the answer.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.