3

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

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
user1137472
  • 345
  • 2
  • 5
  • 20
  • 1
    Your regular expression looks like a date/time format string, not a proper regular expression. – Oded Jan 14 '12 at 21:04
  • 1
    Why are you using a `string` and not a `DateTime` for the `Posted` property? – Oded Jan 14 '12 at 21:06

3 Answers3

7

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}")]
Arnold Zokas
  • 8,306
  • 6
  • 50
  • 76
  • 1
    A .NET date and time format string, specifically (GUID and numbers also have format strings). – Oded Jan 14 '12 at 21:04
  • I will try your answer and report back thank you if it works will give it as a correct solution if not will inform the error – user1137472 Jan 14 '12 at 21:11
  • It does not work after playing around it still gives an error of unrecognized escape sequence – user1137472 Jan 14 '12 at 21:28
  • 2
    @user1137472 Try adding an @ right before the string, like so: `[RegularExpression(@"\d{2,2}/\d{2,2}/\d{4,4} \d{2,2}:\d{2,2}:\d{2,2}")]`. This will make the compiler accept the string even if it contains unrecognized escape sequences (like `\d`). – Michiel van Oosterhout Jan 14 '12 at 21:40
  • Can you please what the numbers like d{2,2) ect... mean fare enough it works but am not the kind of guy who obtains answers and just walks off, I would really like your explanation thank you – user1137472 Jan 15 '12 at 01:08
  • I can't explain all of that regular expression in a comment, but have a look at this website: http://www.regular-expressions.info/quickstart.html. That's where I started learning about regex. – Arnold Zokas Jan 15 '12 at 01:15
  • Thank you but can you give me a general Idea as I thought this website is for people who need help in coding and giving them code and explaining what it does is part of the job if not I am very happy for the code you have given thanks take care and god bless – user1137472 Jan 15 '12 at 01:16
  • you will need to change you your answer slightly as the explanation states the first digit can only be 1 or 2 digits you code stated 2 or 2 digits as this is incorrect you cant have double digit dates all the time unless you use 01 but if you want to allow only 1 you use the following: @"\d{1,2}/\d{1,2}/\d{4} \d{2,2}:\d{2,2}:\d{2,2}", Thank you for the code and website – user1137472 Jan 15 '12 at 01:23
  • Also with your code can add months over 12 is there a way to stop this? same with the date you can add over 31 ? and when u enter all 00/00/00 00:00:00 it crashes is there a way to stop this? – user1137472 Jan 15 '12 at 01:26
0

For a complete guide to client and server validation in MVC (using something like a TextBoxFor), see my answer here: Validate Date in MM/dd/YYYY format in mvc

Community
  • 1
  • 1
Chandler Zwolle
  • 1,635
  • 1
  • 11
  • 8
0

Replace your string in your RegularExpression attribute with a real regular expression. Try one of these from this library site of regex's:

http://regexlib.com/DisplayPatterns.aspx?cattabindex=4&categoryId=5&AspxAutoDetectCookieSupport=1

Try the first one.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
  • Been on this website I did my research before i posted question I cant get them to work even though they seem logical they do not give the correct validation – user1137472 Jan 14 '12 at 21:32