Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular ng-pattern for date not working on IE9

I am trying to use below date (mm/dd/yyyy) validation pattern using ng-pattern in view.

 ng-pattern="/^(\d{4})-(\d{2})-(\d{2})$/"

As soon as I start typing the date the validation error appears and stays even after having valid date such as 10/15/1988

I tried passing it through controller using var (as shown below) but behaves the same:

In Controller:

  $scope.myPattern = "/^(\d{4})-(\d{2})-(\d{2})$/";

In View:

  ng-pattern="{{myPattern}}"

Note: This both approach not working only in IE9

like image 554
seUser Avatar asked Feb 03 '26 20:02

seUser


2 Answers

How about:

 /^((\d{4})-(\d{2})-(\d{2})|(\d{2})\/(\d{2})\/(\d{4}))$/

This will work with both dd/mm/yyyy and yyyy-mm-dd

/^((\d{4})-(\d{2})-(\d{2})|(\d{2})\/(\d{2})\/(\d{4}))$/.test('04/04/1974')
true
/^((\d{4})-(\d{2})-(\d{2})|(\d{2})\/(\d{2})\/(\d{4}))$/.test('1974-04-04')
true
like image 199
Martin Avatar answered Feb 05 '26 10:02

Martin


Your Regex pattern should be ng-pattern="/^(\d{2})\/(\d{2})\/(\d{4})$/". This would match dates that look like mm/dd/yyyy

like image 23
Hacknightly Avatar answered Feb 05 '26 09:02

Hacknightly