Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular Expression Date and Time

Hi people I have the following express validation

    [Required]
    [RegularExpression("{0:d/M/yyyy HH:mm:ss}" , 
    ErrorMessage = "Wrong Syntax")]
    public string Posted { get; set; }` 

But it does not allow the following input which am showing as a example of date and time: 12/12/2011 00:00:00 (I do not want these exact numbers the date and time should allow any numbers which is allowed logically by date and time standards)

I get the error message "Wrong Syntax" even when i input the correct code. What seems to be the problem. Any help will be really appreciated Thank You So Much

like image 200
user1137472 Avatar asked Sep 24 '26 07:09

user1137472


1 Answers

It is because RegularExpressionAttribute expects a Regex pattern and you are providing a .NET string format pattern (MSDN: RegularExpressionAttribute Class).

For basic format validation you would need to use something like:

[RegularExpression(@"\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2}:\d{2,2}")]
like image 161
Arnold Zokas Avatar answered Sep 26 '26 02:09

Arnold Zokas