Is there a way to validate the input field length and check for a specific pattern if it is greater than zero?
Expectation:
- If the input field is empty, display a required message (when touched).
- As the user starts typing, validate the pattern. If the pattern does not match, show a "Please Enter Valid Input" message. If the input field length is 0, only show the required message.
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.1/angular-messages.js"></script>
<div ng-app='ngMessagesExample'>
<form name="test" novalidate ng-submit="test.$valid && submit()">
<div layout-gt-md="row">
<md-input-container class="md-block" flex-gt-xs>
<label>Number</label>
<input required ng-model="user.number" name="number" ng-pattern="/^[0-9]+(\.[0-9]{1,2})?$/" >
<div ng-messages="test.number.$error">
</div>
<div ng-messages="test.$error" ng-show="test.number.$dirty">
<div ng-message="required">number is required</div>
</div>
<span ng-show="!test.number.$valid">Please Enter Valid Input</span>
</md-input-container>
</div>
</form>
</div>
Any assistance would be greatly appreciated!