Regex time greater than 00:00

Asked

Viewed 1,266 times

3

I’m developing a regex validation using the System.ComponentModel.DataAnnotations and RegularExpression from Asp.net mvc C#, in this regex it is necessary to validate unlimited duration time in hours and the time must be greater than 00:00, that is, values can be accepted from 00:01 within minutes(59) and seconds(59).

I managed to get as far as: \d{1,}:[0-5][0-9]

04:00 -- true
00:01 -- true
00:23 -- true
00:04 -- true
04:00 -- true
01:00 -- true
23:00 -- true
21:00 -- true
1:00  -- true
00:61 -- false
00:00 --> problema
57:59 -- true
123:59 -- true

As this is a form, I am only validating a time and not a set of values, as shown in the following image:

inserir a descrição da imagem aqui

I’d like a little help on how to deny the value 00:00 in this regex. As it is in a Regularexpression in Dataannotations, or linked in my model "Duration", I need it to fit in just one validation:

[RegularExpression("^(\d{1,}:[0-5][0-9])$", ErrorMessageResourceName = "CampoObrigatorio")]
public string Duracao { get; set; }
  • My regex validates only hh:mm, where hours can be infinite.

  • is in C#, is in a System.ComponentModel.Dataannotations Regularexpression

  • Yes it is to check.

  • I got it, I added it in the body of the question, it’s just a time and not a set of times.

  • Good evening, did any of the answers solve your problem? If yes, mark the answer that best helped you as correct, if you do not specify what the problem occurred when trying the proposed solutions.

2 answers

3

Use the regular expression ^((?!0+:00)\d{1,}:[0-5][0-9])$

As you can see it’s pretty much the same as yours, with an addition that prevents the match from being "0:00" nor "00:00" and neither "0000:00".

The structure I used is called "Negative Lookahead", what it means:

  • Negative = is the negation of what is described
  • Lookahead = the direction of the negation, which is forward

So (?!0+:00) means: at this point, when looking forward, it may not be possible to match in 0+:00. Note that this structure is an assertive, it does not consume the characters of the string, and so it is possible to follow with the original regular expression its \d{1,}:[0-5][0-9].

1

Using the set; and get; in your case you can create a "mixed REGEX", it would be something like:

^(00:[0-5][1-9]|00:[1-5][0-9]|0[1-9]+[:][0-5][0-9]|[1-9]+[:][0-5][0-9])$

Your code should look like this:

[RegularExpression("^(00:[0-5][1-9]|00:[1-5][0-9]|0[1-9]+[:][0-5][0-9]|[1-9]+[:][0-5][0-9])$", ErrorMessageResourceName = "CampoObrigatorio")]
public string Duracao { get; set; }

Explaining the REGEX:

  • 00:[0-5][1-9] validates times of type 00:01 until 00:59, does not validate 00:10, 00:20, 00:30, etc, so uses the next rule
  • 00:[1-5][0-9] validates times of type 00:10 until 00:59 (validates only type 00:xx then tests the next)
  • 0[1-9]+[:][0-5][0-9] validates times above 01:00 until 09:59
  • [1-9]+[:][0-5][0-9] validates times above 09:59

You can also use to validate larger hours than 00:00 using two different REGEX within the method:

One to check if the time is 00:00:

String horarios = "00:00";
Regex ct1 = new Regex(@"^0+:00$"); //00:00 ou 00000:00 são inválidos
If (!ct1.IsMatch(horarios) {
    //Not found 00:00
} else {
    //Invalid hour
}

And one just to check the schedule:

^\d\d+:\d{2}$

The end result would be something like:

String horarios = "00:00";
Regex ct1 = new Regex(@"^0+:00$"); //00:00 ou 00000:00 são inválidos
Regex ct2 = new Regex(@"^\d+:[0-5][0-9]$");//Valida qualquer horário

If (!ct1.IsMatch(horarios)) {//Se algo como 00:00 ou 00000:00, invalida o código
   If (ct2.IsMatch(horarios)) {
       //Validou
   } else {
       //Hora invalida
   }
} else {
   //Hora invalida
}

Note: As it is not a "list" of times use the sign ^ at the beginning and the $ at the end of the REGEX

  • tried to use, but there are invalid values that cannot pass: 00:61 123:78

  • @Cleber on the "limit", you did not specify in the question. 123:78 is the maximum limit?

  • Sorry. There is no limit in hours. I would just like the duration to be longer than 00:00 to return error message in the form.

  • @Cleber 00:01 would be valid then? You must specify all this in the QUESTION please, Stackoverflow is a Q&A and not a forum, leave the question in more detail and at the same time clear is the minimum to get answers. Grateful

  • All right, buddy, this is my first "Q&A". I could use some. Thanks.

  • @Cleber I’m not being rude, I’m just asking you to edit the question so it’s possible to understand what you need. Thank you

  • I made the edition according to your tips, I wait detailed for collaboration. Thank you.

  • @Cleber I noticed you want to implement the rule in the method set get, then edited the answer.

Show 3 more comments

Browser other questions tagged

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