2019年5月17日 星期五

統一編號驗證

<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;
}

沒有留言:

自訂權限驗證機制

// 使用 filter [Route("api/[controller]")] [ApiController] [Authorize] [TypeFilter(typeof(CustomAsyncAuthorizationFilter))] public c...