问题描述
我正在用angularjs构建挪威语的SSN验证程序,并由于以ECMAScript 5及更高版本为目标时无法使用八进制文字"而收到错误.但是在es3模式下一切正常,请为我解决问题
I am building a norwegaian SSN validator in angularjs and getting the error as 'Octal literals are not available when targeting ECMAScript 5 and higher.' but everything works fine in es3 mode ,please help me with the issue
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;
};
}
}
}
}
推荐答案
这是因为ES3支持使用八进制文字,并以0
开头表示.从ES5开始,由于它们模棱两可并可能导致错误,因此已弃用这些属性.例如:
This is because use of octal literals was supported in ES3 and denoted by a starting 0
. Since ES5 these have been deprecated as these are ambiguous and can lead to errors. E.g.:
var o = 0123;
console.log(o); // 83 decimal
修复:如nnnnnn所指出的,将year >= 00
更改为year >= 0
Fix: change year >= 00
to year >= 0
as pointed out by nnnnnn
注意:ES6将更好地支持0o123
形式的八进制文字: https://github.com/lukehoban/es6features#binary-and-octal-literals
Note: ES6 is going to be better support for octal literals in the form of 0o123
: https://github.com/lukehoban/es6features#binary-and-octal-literals
这篇关于定位ECMAScript 5和更高版本时,八进制文字不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!