モジュール:サンドボックス/A140F6/学校コード検査

モジュールの解説[作成]
local p = {}
function p.Main( frame )
	local targetid = frame.args[1]
	
	-- アルファベット大文字1 + 半角数字12桁から構成されているか確認
	if #targetid == 13 and string.match( targetid , "%u%d%d%d%d%d%d%d%d%d%d%d%d" ) ~= nil then
		-- それら13桁を分割
		local c_type = string.sub( targetid , 1 , 2 )
		local c_pref = string.sub( targetid , 3 , 4 )
		local c_div = string.sub( targetid , 5 , 5 )
		local c_scnf = string.sub( targetid , 6 , 6 )
		local c_ckd = string.sub( targetid , 13 , 13 )
		
		-- 最初2桁が「学校種」のいずれかに合致するか確認
		if ( c_type == "A1" or c_type == "A2" or c_type == "B1" or c_type == "C1" or c_type == "C2" or c_type == "D1" or c_type == "D2" or c_type == "E1" or c_type == "F1" or c_type == "F2" or c_type == "G1" or c_type == "H1" or c_type == "H2" ) and
		-- 3~4桁目が都道府県番号01~47のいずれかに合致するか確認
		( string.match( c_pref , "0[1-9]" ) ~= nil or string.match( c_pref , "[1-3][0-9]" ) ~= nil or string.match( c_pref , "4[0-7]" ) ~= nil ) and
		-- 5桁目が「設置区分」1〜3のいずれかに合致するか確認
		( c_div == "1" or c_div == "2" or c_div == "3" ) and
		-- 学校番号の確認。2桁目以降の数字に制限はないため、1桁目が0でないことのみ確認
		c_scnf ~= "0" then
			
			-- 検査数字の確認
			local ck_1 = 0
			local ck_2 = ""
				if string.sub( targetid , 1 , 1 ) == "A" then
					ck_2 = 1
				elseif string.sub( targetid , 1 , 1 ) == "B" then
					ck_2 = 2
				elseif string.sub( targetid , 1 , 1 ) == "C" then
					ck_2 = 3
				elseif string.sub( targetid , 1 , 1 ) == "D" then
					ck_2 = 4
				elseif string.sub( targetid , 1 , 1 ) == "E" then
					ck_2 = 5
				elseif string.sub( targetid , 1 , 1 ) == "F" then
					ck_2 = 6
				elseif string.sub( targetid , 1 , 1 ) == "G" then
					ck_2 = 7
				elseif string.sub( targetid , 1 , 1 ) == "H" then
					ck_2 = 8
				end
			local ck_3 = tonumber( string.sub( targetid , 2 , 2 ) )
			local ck_4 = tonumber( string.sub( targetid , 3 , 3 ) )
			local ck_5 = tonumber( string.sub( targetid , 4 , 4 ) )
			local ck_6 = tonumber( string.sub( targetid , 5 , 5 ) )
			local ck_7 = tonumber( string.sub( targetid , 6 , 6 ) )
			local ck_8 = tonumber( string.sub( targetid , 7 , 7 ) )
			local ck_9 = tonumber( string.sub( targetid , 8 , 8 ) )
			local ck_10 = tonumber( string.sub( targetid , 9 , 9 ) )
			local ck_11 = tonumber( string.sub( targetid , 10 , 10 ) )
			local ck_12 = tonumber( string.sub( targetid , 11 , 11 ) )
			local ck_13 = tonumber( string.sub( targetid , 12 , 12 ) )
			
			local ckd_1 = ck_1
			local ckd_2 = ""
			local ckd_3 = ck_3
			local ckd_4 = ""
			local ckd_5 = ck_5
			local ckd_6 = ""
			local ckd_7 = ck_7
			local ckd_8 = ""
			local ckd_9 = ck_9
			local ckd_10 = ""
			local ckd_11 = ck_11
			local ckd_12 = ""
			local ckd_13 = ck_13
			
			if ck_2 * 2 >= 10 then
				ckd_2 = ck_2 * 2 - 9
			else
				ckd_2 = ck_2 * 2
			end
			
			if ck_4 * 2 >= 10 then
				ckd_4 = ck_4 * 2 - 9
			else
				ckd_4 = ck_4 * 2
			end

			if ck_6 * 2 >= 10 then
				ckd_6 = ck_6 * 2 - 9
			else
				ckd_6 = ck_6 * 2
			end

			if ck_8 * 2 >= 10 then
				ckd_8 = ck_8 * 2 - 9
			else
				ckd_8 = ck_8 * 2
			end

			if ck_10 * 2 >= 10 then
				ckd_10 = ck_10 * 2 - 9
			else
				ckd_10 = ck_10 * 2
			end

			if ck_12 * 2 >= 10 then
				ckd_12 = ck_12 * 2 - 9
			else
				ckd_12 = ck_12 * 2
			end
			
			local ckd_sum = ckd_1 + ckd_2 + ckd_3 + ckd_4 + ckd_5 + ckd_6 + ckd_7 + ckd_8 + ckd_9 + ckd_10 + ckd_11 + ckd_12 + ckd_13
			local ckd_mod = ckd_sum % 10
			local ckd_result = ""
			if ckd_mod == 0 then
				ckd_result = 0
			else
				ckd_result = 10 - ckd_mod
			end
			
			if ckd_result == tonumber( c_ckd ) then
				return frame.args[1]
			else
				return "[[Category:Infoboxに不備のある学校記事]]"
			end
		else
			return "[[Category:Infoboxに不備のある学校記事]]"
		end
	else
		return "[[Category:Infoboxに不備のある学校記事]]"
	end
end
return p