# 台灣統一編號驗證格式 ```javascript= uniformNumbers_verification = (uniformNumbers) =>{ // 檢查字元是否符合規則 const regex = /^[0-9]{8}$/; // 統一編號 邏輯乘數 const logicMultipliers = [1, 2, 1, 2, 1, 2, 4, 1]; // 計算陣列總和 const sum = (numbers) =>{ const initialValue = 0; const sumWithInitial = numbers.reduce( (accumulator, currentValue) => Number(accumulator) + Number(currentValue), initialValue, ); return sumWithInitial } if(!uniformNumbers){ return '統編未填寫' } if (uniformNumbers.length !== 8 || !regex.test(uniformNumbers)){ return '統編格式不正確' } let logicProductArr = [] let logicProduct = 0; // 通一編號倒數第二位為7時,乘積之和最後第二位數取0或1均可,其中之一和能被5整除,則符合統編邏輯 if (uniformNumbers[6] == "7") { for (let i = 0; i < uniformNumbers.length ; i++) { if (i != 6) { logicProductArr.push(parseInt(uniformNumbers[i]) * logicMultipliers[i]) } } } else { for (let i = 0; i < uniformNumbers.length ; i++) { logicProductArr.push(parseInt(uniformNumbers[i]) * logicMultipliers[i]); } } for(const item of logicProductArr){; logicProduct += sum((item.toString()).split('')) } if (uniformNumbers[6] === '7' && (logicProduct % 5 === 0 || (logicProduct + 1) % 5 === 0)) { return '統編驗證通過'; } else if (logicProduct % 5 === 0) { return '統編驗證通過'; } return '統編驗證失敗' } ``` ###### 持續更新中! ###### 以上為自學中的筆記如有錯誤,懇請留言指正,感謝!
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up