Wednesday, June 5, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  139] [ 3]  / answers: 1 / hits: 12256  / 10 Years ago, tue, april 8, 2014, 12:00:00

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


More From » regex

 Answers
5

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

[#46193] Tuesday, April 8, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lincolnx

Total Points: 602
Total Questions: 90
Total Answers: 94

Location: Saint Lucia
Member since Wed, Feb 8, 2023
1 Year ago
;