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>');

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes it did fixed the issue after updating the regex. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.