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

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>
||||||
  • 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 – sij Dec 4 '14 at 11:35
  • substr is a method of type "string", so "value" or "cpr" should be strings. – Pavel Kutakov Dec 5 '14 at 7:31
  • 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); – sij Dec 5 '14 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.