// 日付フォーマット
onblur="FormatDateYYYYMMDD(this, this.value, 8)"
で呼び出すと、YYYYMMDD -> YYYY/MM/DD となる
function FormatDateYYYYMMDD(obj, val, len){
str_val = val;
// ********** 半角数値チェック ********** //
checkFlg = 0;
arrNumber = new Array("1","2","3","4","5","6","7","8","9","0","/");
for(i = 0; i < str_val.length; i++) {
c_val = str_val.substring(i, i + 1);
for(j = 0; j < arrNumber.length; j++){
c_num = arrNumber[j];
if (c_val == c_num) {
checkFlg++;
}
}
}
if (str_val.length != checkFlg) {
obj.value = val;
return;
}
// ********** 半角数値チェック ********** //
// ********** 文字列から[/]を除去する ********** //
ret_val = '';
for(i = 0; i < val.length; i++){
c_val = val.substring(i, i + 1);
if (c_val == '/') {
c_dumy = '';
} else {
c_dumy = c_val;
}
ret_val = ret_val + c_dumy;
}
str_val = ret_val;
// ********** 文字列から[/]を除去する ********** //
// ********** 文字列長チェック ********** //
if (str_val.length != len) {
obj.value = val;
return;
}
// ********** 文字列長チェック ********** //
// ********** 日付チェック ********** //
n_y = str_val.substring(0,4);
n_m = str_val.substring(4,6);
n_d = str_val.substring(6,8);
monthEndDay = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
cal = new Date;
n_day = 0;
// 月チェック
if ((n_m < 1) || (12 < n_m)) {
obj.value = val;
return;
}
// うるう年計算
cal.setYear(n_y);
cal.setMonth(n_m - 1);
n_day = monthEndDay[n_m - 1];
if ((n_m == 2)&&(((n_y%4 == 0)&&(n_y%100 != 0))||(n_y%400 == 0))){
n_day = 29;
}
if ((n_d < 0) || (n_day < n_d)) {
obj.value = val;
return;
}
// ********** 日付チェック ********** //
// ********** フォーマット文字列返還 ********** //
obj.value = n_y + "/" + n_m + "/" + n_d;
// ********** フォーマット文字列返還 ********** //
}
仕事に関係する情報のメモ 技術、プロジェクト管理など
火曜日, 6月 10, 2008
登録:
コメントの投稿 (Atom)
0 件のコメント:
コメントを投稿