I have one input text field that takes Year value.
I used
ng-pattern="/^[0-9]{4}$/" for enter year value in 4 digit.But How can I check input year is between from 1950 to current year.
2 Answers
You can use an input[number] using min and max validation
<body ng-app="app" ng-controller="Ctrl as vm">
<form name="form">
<input type="number" ng-model="year" name="year" min="1950" max="{{vm.currentYear}}"> <br/>
<span ng-if="form.year.$error.min">Year should be at least 1950</span>
<span ng-if="form.year.$error.max">Year can not be in the future!</span>
</form>
<script>
angular.module('app', []).controller("Ctrl", function() {
this.currentYear = new Date().getFullYear();
});
</script>
</body>
This will set the min ormax validation $error key on year in case someone enters a year that is not within these bounds.
Comments
Try This
$scope.currentYear = new Date().getFullYear();
HTML
<input type="number" ng-model="value" min="1950" max="{{currentYear}}">
1 Comment
Robin-Hoodie
You forgot
name and ng-model attributes. No validation will happen without these