2

I have the below text field where I'm using the ng-pattern for validation and displaying the message using ng-message. The validation works fine with the both the regex (/^\d+$/ or /^[0-9]+$/), but for some reason the regex (/^\d+$/) is returning false even for numeric inputs while using along with the template caching. I couldn't figure out the issue between these two approach. Please find the below templateCaching output. Any help is appreciated.

input type="text" ng-model="appId" name="appId" class="new-app" ng-pattern="/^\d+$/" placeholder="Enter an Application ID" required

Message :

div ng-message="pattern" class="error-messages"

Not Working with templateCaching:

input type="text" ng-model="appId" name="appId" class="new-app" ng-pattern="/^\d+$/" placeholder="Enter an Application ID" required

Working with both templateCaching and as a separate view file:

input type="text" ng-model="appId" name="appId" class="new-app" ng-pattern="/^[0-9]+$/" placeholder="Enter an Application ID" required

$templateCache.put('/views/layouts/test.html', '<input type="text" ng-model="appId" name="appId" class="new-app" ng-pattern="/^[0-9]+$/" placeholder="Enter an Application ID" required> <input type="text" ng-model="appId" name="appId" class="new-app" ng-pattern="/^\d+$/" placeholder="Enter an Application ID" required>');

Karthilxg
  • 21
  • 4

1 Answers1

0

I had similar issue with templateCache and it was due to having special characters in the pattern like forward slashes and my validatioin was failing. Escaping them fixed the issue: so if you have ng-pattern="/^\d+$/" in html file than it becomes ng-pattern="/^\\d+$/" in your templateCache statement.