Regex for validating date in dd-Mmm-yyyy format

后端 未结 3 717
难免孤独
难免孤独 2021-01-27 09:01

I have an text input box which needs to be validated. The user should be only able to enter the date in dd-Mmm-yyyy format. ex: 01-Jun-2013, 31-Aug-2015 and so on. Or they shoul

相关标签:
3条回答
  • 2021-01-27 09:44

    T followed by "+" and a number ranging from "1 to 99"

    Try with: T\+?\d{0,2} where:

    1. T literal "T";
    2. \+? - zero or one plus sign "+" with escape backslash (or other escape character, depends on language) - without it it will be treated as metacharacter '+'; the additional '?' sing means, that character before that could appear, but it is not necessary
    3. \d{0,2} - from zero to two digits;

    DEMO

    0 讨论(0)
  • 2021-01-27 09:49

    Not sure what you mean by T+1, etc, but here's the bare minimum that does what you need:

    ^[01][0-9]-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{4}$

    You can also use the i flag to make the whole thing case insensitive.

    http://www.regexr.com/3bdeu

    0 讨论(0)
  • 2021-01-27 09:58

    If I correctly understand your question, I added the possibility to enter someting matching (T\+[0-9]+) to your original regex.

    ^((([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4})|(T\+[0-9]+)$
    
    0 讨论(0)
提交回复
热议问题