0

Im trying to make a input field that need a 10 digit number (danish SSN number) and i need to grab the "year" number and add to numbers to it.

a number can be 1255905487

where i need to grab 90 and add 19 to it, so its 1990 etc. Im just not sure how to do this in the best way

So any help in the right direction would be nice :)

I have made a watcher for it

        $scope.$watch(function() {
        return $scope.cpr;
    }, function(age) {
        console.log("change detected: " + age);
    });

1 Answer 1

1

First way, make watcher as you already did:

  $scope.$watch(function (){
    return $scope.cpr;
  },
  function (value){
    var shortYear=parseInt(value.substr(4,2));
    if (shortYear>50)
      $scope.age=1900+shortYear;
    else
      $scope.age=2000+shortYear;
  });

Second way, create scope function that will calculate it:

  $scope.getAge=function (){
    var shortYear=parseInt($scope.cpr.substr(4,2));
    if (shortYear>50)
      return 1900+shortYear;
    else
      return 2000+shortYear;
  }

And use it:

  <input type="text" ng-model="cpr" />
  age:
  <span>{{getAge()}}</span>
Sign up to request clarification or add additional context in comments.

3 Comments

Wow, that seems like a good way to do it, first way and second way is 2 different solutions, right? right now i got this error TypeError: Cannot read property 'substr' of undefined
substr is a method of type "string", so "value" or "cpr" should be strings.
ahh Thanks for the help, and for future i used this to debug what the type was var test = toString(value); console.log("TYPEOF"); console.log(typeof test);

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.