定位ECMAScript 5及更高版本时,八位字面值不可用

时间:2015-04-06 07:26:45

标签: javascript angularjs angularjs-directive typescript

我正在angularjs中构建一个norwegaian SSN验证器,并将错误视为“在定位ECMAScript 5及更高版本时,八位字面值不可用”。但是在es3模式下一切正常,请帮我解决问题

module ec.directives {
    export function norwegianSsnValidator(){
      return {
        restrict: 'A',
        require: 'ngModel',
        link: function(scope, element, attrs, ctrl){
            ctrl.$validators.invalidSSN = function(ssn:string){
                if(typeof ssn !== "string"){
                    return false;
                }
                var pno = ssn, day, month, year,
                    ind, c1, c2, yearno;
                // Check length
                if( pno.length != 11 )
                    return false;

                // Split
                day = pno.substr(0,2);
                month = pno.substr(2,2);
                year = pno.substr(4,2);
                ind = pno.substr(6,3);
                c1 = pno.substr(9,1);
                c2 = pno.substr(10,1);
                yearno = parseInt(year);

                if( ind > 0 && ind < 500 ) {
                    yearno += 1900;
                } else if( ind > 499 && ind < 750 && year > 55 && year < 100) {
                    yearno += 1800;
                } else if( ind > 499 && ind < 999 && year >= 00 && year < 40) {
                    yearno += 2000;
                } else if( ind > 899 && ind < 999 && year > 39 && year < 100) {
                    yearno += 1900;
                } else {
                    return false;
                }
                var d1, d2,m1,m2,a1,a2,i1,i2,i3, c1calc, c2calc;
                d1 = parseInt(day.substr(0,1));
                d2 = parseInt(day.substr(1,1));
                m1 = parseInt(month.substr(0,1));
                m2 = parseInt(month.substr(1,1));
                a1 = parseInt(year.substr(0,1));
                a2 = parseInt(year.substr(1,1));
                i1 = parseInt(ind.substr(0,1));
                i2 = parseInt(ind.substr(1,1));
                i3 = parseInt(ind.substr(2,1));

                // Calculate control check c1
                c1calc = 11 - (((3*d1) + (7*d2) + (6*m1) + m2 + (8*a1) + (9*a2) + (4*i1) + (5*i2) + (2*i3)) % 11);
                if( c1calc == 11 )
                    c1calc = 0;
                if( c1calc == 10 )
                    return false;
                if( c1 != c1calc )
                    return false;

                // Calculate control check c2
                c2calc = 11 - (((5*d1) + (4*d2) + (3*m1) + (2*m2) + (7*a1) + (6*a2) + (5*i1) + (4*i2) + (3*i3) + (2*c1calc)) % 11);
                if( c2calc == 11 )
                    c2calc = 0;
                if( c2calc == 10 )
                    return false;
                if( c2 != c2calc )
                    return false;

                return true;
            };
        }
    }
  }
}

1 个答案:

答案 0 :(得分:6)

这是因为在ES3中支持使用八进制文字,并以起始0表示。由于ES5已被弃用,因为它们含糊不清并可能导致错误。例如:

var o = 0123;
console.log(o); // 83 decimal

修正:按照nnnnnn的指示将year >= 00更改为year >= 0

注意:ES6将以0o123的形式更好地支持八进制文字:https://github.com/lukehoban/es6features#binary-and-octal-literals