Regular Expression Date and Time

房东的猫 提交于 2019-12-24 00:35:01

问题


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


回答1:


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}")]



回答2:


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



来源:https://stackoverflow.com/questions/8865323/regular-expression-date-and-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!