2011年3月24日木曜日

年度関数、ほぼ完成

年度関数。

年度の入力ヘルパーです。

動作にはJQueryが必要です。



(function($) {

$.fn.keisan = function(options){

function calculate(obj){
var count = $(obj).val().length;

//年度セット
myD = new Date();
myY = myD.getYear();     // 年
myZ = myY-1;     // 年
var vx = $("#tyr").attr("value");

vx = vx.replace(/[^0-9\-]/g,"");
$("#tyr").attr("value",vx);

if(vx.match(/\-$/)){
var yr = (vx.substring(0,4));
if(yr <= "2000"){yr = myY;}
else{yr--;}
$("#tyr").attr("value",yr);
}

else if(vx >= 3000){
vx = "";
$("#tyr").attr("value",vx);
}
else if(count==2 && vx >= 40){
vx = "19" + vx;
$("#tyr").attr("value",vx);
}
else if(count==2 && vx !=19 && vx !=20 && vx < 40){
vx = "20" + vx;
$("#tyr").attr("value",vx);
}
};

this.each(function() {
calculate(this);
$(this).keyup(function(){calculate(this)});
$(this).change(function(){calculate(this)});
});

};

})(jQuery);

0 件のコメント:

コメントを投稿