AngularJS Date Pattern Validation Changes from v1.3 to v1.4

Issue

I recently asked this question about the changes to RegExp pattern validation that were introduced in AngularJS v1.3. The answer I received apparently solved my problem, but now I am trying to apply that approach, and I see that the behavior is again different in AngularJS v1.4.

Specifically, I want to apply pattern validation to a date input field, but the validation RegExp will be exposed as a property of the model, instead of being hard-coded into the form markup.

As suggested, I am specifying the name of the model property in the ng-pattern attribute …

<input type="date" ng-model="myDate" name="myDate" ng-pattern="control.dateRegex" />

… and exposing the validation RegExp as a property of the model:

$scope.control = {
    dateRegex: /^2015-\d+-\d+$/
};

This JSFiddle shows it working correctly with AngularJS v1.3 and this one demonstrates that the same implementation does not work with v1.4. I am unable to find any documentation that describes the correct implementation for use with v1.4.

Any suggestions please?

Solution

After asking a similar question on the AngularJS issues forum, I learned that this behavior is specific to date input validation. It arises because the model property used for date input binding has changed from a String to a Date object, which means that is no longer possible to use a RegExp to validate it.

It seems that the AngularJS team recognize this as a bug and that we can expect a fix in a forthcoming release. I will monitor the issue and update this thread when there is some progress.

Answered By – Tim Coulter

Answer Checked By – Marie Seifert (AngularFixing Admin)

Leave a Reply

Your email address will not be published.