<input uniformvalidate ng-model='uniform'><span style='color:red' ng-show="form1.$error.uniform">*</span>
.directive("uniformvalidate", function () {
return {
restrict: 'A',
require: "ngModel",
link: function (scope, element, attributes, ngModel) {
ngModel.$validators.uniform = function (modelValue, viewValue) {
return 檢查統一編號(modelValue);
}
}
};
})
function 檢查統一編號(NO) {
if (!NO) return true;
var SUM = 0;
if (NO.length != 8) {
return false;
}
var 統編檢查陣列 = '12121241'.split('');
var 統編字元陣列 = NO.split("");
for (i = 0; i <= 7; i++) {
if (NO.charCodeAt() < 48 || NO.charCodeAt() > 57) {
return false;
}
SUM += 兩位數相加(統編字元陣列[i] * 統編檢查陣列[i]);
}
if (SUM % 10 == 0) return true;
else if (統編字元陣列[6] == 7 && (SUM + 1) % 10 == 0) return true;
else return false;
}
沒有留言:
張貼留言