$(function () {
//URLを取得
const href = location.href;
//条件をかく
const match = href.match(/(\d{2})(\d{2})(\d{2})/);
//先頭の数字
let headnum = match[1];
//年度のため4月より前なら減らす
if (match[2] < 4) {
headnum -= 1;
}
headnum %= 10;
$('input[name="mem_number"]').on('blur', function () {
const num = $(this).val().slice(0, 1);
const length = $(this).val().length;
if (Number(num) !== Number(headnum) || Number(length) !== 4) {
alert('会員番号は' + headnum + 'から始まる4桁の数字を入力してください');
$(this).val('');
}
});
});
javascriptでURLから条件で処理を変更
URLを取得してマッチさせてあとは条件文で何とかする。
サンプルはURLに日付を記述しておく事で年度毎で会員番号が毎回変わるバリデート
コメント