My goal is to validate a date in the format YYYY/MM/DD using a regular expression ng-pattern. When I use the code below in the UI, it works perfectly fine.
<input type="text" class="k-fill" ng-pattern="/((^[1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))\/([0]{1}[1-9]{1}|[1]{1}[0-2]{1})\/([0]{1}[1-9]{1}|[1,2]{1}\d{1}|[3]{1}[0,1]{1})$/" ng-model="Request.ExpDate" id="ExceptedDate" name="ExceptedDate" ng-readonly="true" required />
However, I would like to validate this pattern within a function so that I can display a validation message in a pop-up. In order to achieve this, I implemented the following code in one of my JS files.
var str = Request.ExpDate;
var x = '/((^[1]{1}[9]{1}[9]{1}\d{1})|([2-9]{1}\d{3}))\/([0]{1}[1-9]{1}|[1]{1}[0-2]{1})\/([0]{1}[1-9]{1}|[1,2]{1}\d{1}|[3]{1}[0,1]{1})$/';
var patt = new RegExp(x);
var res = patt.test(str);
If res returns false, I intend to show a message. However, the issue lies in the fact that it returns false for all dates that are actually in the correct format.
Could you kindly explain why the regexp works correctly with ng-pattern but does not function properly within the JS function?