Regex time greater than 00:00

3

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

I was able to get up at: \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 just validating a time and not a set of values, as follows:

Iwouldlikehelpwithdenyingthe00:00valueinthisregex.AsitgetsinaRegularExpressioninDataAnnotations,orisboundinmy"Duration" model, I need to fit in just one validation:

[RegularExpression("^(\d{1,}:[0-5][0-9])$", ErrorMessageResourceName = "CampoObrigatorio")]
public string Duracao { get; set; }
    
asked by anonymous 28.12.2014 / 18:57

2 answers

3

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

As you can see, it's practically the same as yours, with an addition that prevents the match from being "0:00" nor "00:00" nor "0000:00" .

The structure I used is called "Negative lookahead" , which means:

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

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

    
28.01.2015 / 01:12
1

Using set; and get; in your case, you can create a "mixed REGEX", it would look 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 REGEX:

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

You can also use validate time greater than 00:00 by 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 another just to check the time:

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

The end result would look 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: Since it is not a "list" of times, use the ^ sign at the beginning and $ at the end of REGEX

    
28.12.2014 / 19:15