ng-pattern to validate dd/mm/yy format date in angularjs
16,053
Solution 1
regex for that would be
ng-pattern="/\d\d/\d\d/\d\d\d\d/"
Solution 2
The regex will be
/^(((0[1-9]|[12]\d|3[01])-(0[13578]|1[02])-((19|[2-9]\d)\d{2}))|((0[1-9]|[12]\d|30)-(0[13456789]|1[012])-((19|[2-9]\d)\d{2}))|((0[1-9]|1\d|2[0-8])-02-((19|[2-9]\d)\d{2}))|(29-02-((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))$/
This is valid for leap years as well. To use this in your application, you can either use it inline with ng-pattern
or use it as
$scope.pattern = new RegExp(PatternAsString);
<input ng-model="foo" ng-pattern="pattern">
Solution 3
Use below pattern. It only validates date format "yyyy/MM/dd".
ng-pattern = ^[0-9]{4}[\/][0-9]{2}[\/][0-9]{2}$
Author by
Dhana Lakshmi
Updated on June 04, 2022Comments
-
Dhana Lakshmi almost 2 years
i have a date picker which accepts date in dd/mm/yy format can someone please say what is the regular expression should be place in ng-pattern to match the same date will contain only / no other special character example:12/12/2015
-
Sebastianb over 7 yearsjust FYI, this regexp accepts dates in the dd-mm-yyyy, not dd/mm/yyyy which is what OP was asking.
-
WhatsThePoint over 6 yearsCould you please format your code by highlighting it and hitting Ctrl+K
-
Dharman over 4 yearsIvarni has not asked the question. Why are you addressing him?