How can ng-pattern be not triggered when the field is pristine?

54 Views Asked by At

I use ng-pattern to have validation on one of text inputs. Is there a way to trigger the validation only if the text input has been modified? Or in other words, the validation should not be triggered when text input is pristine, ie. not modified by the user (perhaps by setPristine(true). Not sure, but this could be done by setting the field as valid on init. Here's a part of my code (from *.jade file):

 +text_input('Flat number', 'flatNumber', '', null, {
        tooltip: '{{ "invalid_flat_number_tooltip" | translate }}'
      })(ng-disabled='!editable', ng-readonly='!editable', ng-pattern="/^(?:\\d{1,3}[A-Z]?(?:\\/\\d{1,3})?)?$/", label-s)
1

There are 1 best solutions below

0
Petr Averyanov On

ng-pattern supports dynamic value, so e.g. you can have this:

ng-pattern="$ctrl.pattern" ng-change="$ctrl.setPattern()"

const realPattern = /.../;
vm.pattern = ''
vm.setPattern = () => vm.pattern = realPattern;