1

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 2

1

You can use an input[number] using min and max validation

Plunkr

 <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.

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

Comments

0

Try This

$scope.currentYear = new Date().getFullYear();

HTML

<input type="number" ng-model="value" min="1950" max="{{currentYear}}">

1 Comment

You forgot name and ng-model attributes. No validation will happen without these

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.