Angular ng-pattern for date not working on IE9

seUser picture seUser · Apr 8, 2014 · Viewed 11.7k times · Source

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

Answer

Martin picture Martin · Apr 8, 2014

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