常用JS函数(3)
//------------------------------------------------------------
//適当な日付をMM/DDの形式に変換
// 引数: 適当な日付
// 返値: MM/DDの形式の日付
function convFazyDatemd(sdate){
var retDate = "";
var mmdd;
if(isNaN(sdate)){
retDate = sdate.replace(/\D/g, "/");
var tmp = retDate.split("/");
if(tmp.length < 2) return "";
mmdd = tmp.slice(0,2);
}else{
var len = sdate.length;
if(len < 3) return "";
mmdd = new Array(
sdate.substr(0, len - 2),
sdate.substr(len - 2, 2)
);
}
if((mmdd[0].length == 0) || (mmdd[1].length == 0)) return "";
if(mmdd[0].length == 1) mmdd[0] = "0" + mmdd[0];
if(mmdd[1].length == 1) mmdd[1] = "0" + mmdd[1];
return mmdd.join("/");
}
//適当な日付をMM/DDの形式に変換
// 引数: 適当な日付
// 返値: MM/DDの形式の日付
function convFazyDatemd(sdate){
var retDate = "";
var mmdd;
if(isNaN(sdate)){
retDate = sdate.replace(/\D/g, "/");
var tmp = retDate.split("/");
if(tmp.length < 2) return "";
mmdd = tmp.slice(0,2);
}else{
var len = sdate.length;
if(len < 3) return "";
mmdd = new Array(
sdate.substr(0, len - 2),
sdate.substr(len - 2, 2)
);
}
if((mmdd[0].length == 0) || (mmdd[1].length == 0)) return "";
if(mmdd[0].length == 1) mmdd[0] = "0" + mmdd[0];
if(mmdd[1].length == 1) mmdd[1] = "0" + mmdd[1];
return mmdd.join("/");
}
//日付入力フィールドのフォーカスが離れたら、正しい形の日付に変換する
function txtDate_onBlurmd(ctrl){
var ymd = convFazyDatemd(trim(ctrl.value));
if(ymd.length > 0){
ctrl.value = ymd;
}
}
//------------------------------------------------------------
//適当な日付をYYYY/MM/DDの形式に変換
// 引数: 適当な日付
// 返値: YYYY/MM/DDの形式の日付
function convFazyDate(sdate){
var retDate = "";
var ymd;
if(isNaN(sdate)){
retDate = sdate.replace(/\D/g, "/");
var tmp = retDate.split("/");
if(tmp.length < 3) return "";
ymd = tmp.slice(0,3);
}else{
var len = sdate.length;
if(len < 5) return "";
ymd = new Array(
sdate.substr(0, len - 4),
sdate.substr(len - 4, 2),
sdate.substr(len - 2, 2)
);
}
if((ymd[0].length == 0) || (ymd[1].length == 0) || (ymd[2].length == 0)) return "";
if(ymd[0].length == 1) ymd[0] = "200" + ymd[0];
if(ymd[0].length == 2) ymd[0] = "20" + ymd[0];
if(ymd[0].length == 3) ymd[0] = "2" + ymd[0];
if(ymd[1].length == 1) ymd[1] = "0" + ymd[1];
if(ymd[2].length == 1) ymd[2] = "0" + ymd[2];
return ymd.join("/");
}
//日付入力フィールドのフォーカスが離れたら、正しい形の日付に変換する
function txtDate_onBlur(ctrl){
var ymd = convFazyDate(trim(ctrl.value));
if(ymd.length > 0){
ctrl.value = ymd;
}
}
//金額をカンマ編集して右に詰める
function txtConmma_onBlur(ctrl){
var kin = convConmma(trim(ctrl.value));
if(kin.length > 0){
ctrl.value = kin;
}
ctrl.style.textAlign = "right";
}
//年月入力フィールドのフォーカスが離れたら、正しい形の年月に変換する
function txtYearMonthCheck(sDate){
var withDummyDay = sDate + "/01"; /* ダミーで01日を設定 */
var tempDate = convFazyDate(withDummyDay);
if(tempDate == ""){
withDummyDay = sDate + "01"; /* "/"の無い01日を設定 */
tempDate = convFazyDate(withDummyDay);
if(tempDate == ""){
return ""; /* あきらめる */
}
}
var result = txtYearMonthDayCheck(tempDate)
if(result == ""){
return "";
}
else{
return result.substr(0,7); /* YYYY/MM/DDである前提 */
}
}
//年月入力フィールドのフォーカスが離れたら、正しい形の年月に変換する
function txtYearMonth_onBlur(ctrl, msg){
if(ctrl.value.length == 0) return;
var ym = txtYearMonthCheck(trim(ctrl.value));
if(ym.length == 0) {
alert(msg);
ctrl.focus();
ctrl.select();
}
else {
ctrl.value = ym
}
}
//月日入力フィールドのフォーカスが離れたら、正しい形の月日に変換する
function txtMonthDayCheck(sDate){
var withDummyYear = "2000/" + sDate; /* 2000年なので2/29許容 */
var result = txtYearMonthDayCheck(withDummyYear)
if(result == ""){
return "";
}
else{
return result.substr(5,5); /* YYYY/MM/DDである前提 */
}
}
function txtDate_onBlurmd(ctrl){
var ymd = convFazyDatemd(trim(ctrl.value));
if(ymd.length > 0){
ctrl.value = ymd;
}
}
//------------------------------------------------------------
//適当な日付をYYYY/MM/DDの形式に変換
// 引数: 適当な日付
// 返値: YYYY/MM/DDの形式の日付
function convFazyDate(sdate){
var retDate = "";
var ymd;
if(isNaN(sdate)){
retDate = sdate.replace(/\D/g, "/");
var tmp = retDate.split("/");
if(tmp.length < 3) return "";
ymd = tmp.slice(0,3);
}else{
var len = sdate.length;
if(len < 5) return "";
ymd = new Array(
sdate.substr(0, len - 4),
sdate.substr(len - 4, 2),
sdate.substr(len - 2, 2)
);
}
if((ymd[0].length == 0) || (ymd[1].length == 0) || (ymd[2].length == 0)) return "";
if(ymd[0].length == 1) ymd[0] = "200" + ymd[0];
if(ymd[0].length == 2) ymd[0] = "20" + ymd[0];
if(ymd[0].length == 3) ymd[0] = "2" + ymd[0];
if(ymd[1].length == 1) ymd[1] = "0" + ymd[1];
if(ymd[2].length == 1) ymd[2] = "0" + ymd[2];
return ymd.join("/");
}
//日付入力フィールドのフォーカスが離れたら、正しい形の日付に変換する
function txtDate_onBlur(ctrl){
var ymd = convFazyDate(trim(ctrl.value));
if(ymd.length > 0){
ctrl.value = ymd;
}
}
//金額をカンマ編集して右に詰める
function txtConmma_onBlur(ctrl){
var kin = convConmma(trim(ctrl.value));
if(kin.length > 0){
ctrl.value = kin;
}
ctrl.style.textAlign = "right";
}
//年月入力フィールドのフォーカスが離れたら、正しい形の年月に変換する
function txtYearMonthCheck(sDate){
var withDummyDay = sDate + "/01"; /* ダミーで01日を設定 */
var tempDate = convFazyDate(withDummyDay);
if(tempDate == ""){
withDummyDay = sDate + "01"; /* "/"の無い01日を設定 */
tempDate = convFazyDate(withDummyDay);
if(tempDate == ""){
return ""; /* あきらめる */
}
}
var result = txtYearMonthDayCheck(tempDate)
if(result == ""){
return "";
}
else{
return result.substr(0,7); /* YYYY/MM/DDである前提 */
}
}
//年月入力フィールドのフォーカスが離れたら、正しい形の年月に変換する
function txtYearMonth_onBlur(ctrl, msg){
if(ctrl.value.length == 0) return;
var ym = txtYearMonthCheck(trim(ctrl.value));
if(ym.length == 0) {
alert(msg);
ctrl.focus();
ctrl.select();
}
else {
ctrl.value = ym
}
}
//月日入力フィールドのフォーカスが離れたら、正しい形の月日に変換する
function txtMonthDayCheck(sDate){
var withDummyYear = "2000/" + sDate; /* 2000年なので2/29許容 */
var result = txtYearMonthDayCheck(withDummyYear)
if(result == ""){
return "";
}
else{
return result.substr(5,5); /* YYYY/MM/DDである前提 */
}
}