I want to use regex to validate text fields in JSF. Could someone tell me the pattern for hours (hh: mm) and date (dd / mm / yyyy). I tried the following patterns, but without knowing anything.
<f:validateRegex pattern="[0-9][0-9]:[0-9][0-9]" />
<f:validateRegex pattern="[0-9][0-9]/[0-9][0-9]/[0-9][0-9][0-9][0-9]" />
Best How To :
That's fairly correct, but you can adjust things to make them more realistic.
[0-2]?[0-9]:[0-5][0-9]" would be more realistic for time, as there is no
32:79 and what not. Also, the first can be optional to make
[0-3]?[0-9]\/[0-1]?[0-9]\/[1-2][0-9][0-9][0-9] for date, as there can be no 42th of april and so on. Also, escape the "/", which can be treated as delimiter.
Note that this is still imperfect because of "39 of april" being aviable and so on. You can make longer and more difficult regexes to avoid this, but it's better to use built in functions or make your own functions for this that evaluate each part of the valiable.
For example, february will have 28 days, 30th of ferbuary is not a viable date, but regex is not the best tool to deal with these situations.