µ±Ç°Î»Ö㺠´úÂëÃÔ >> JavaScript >> JSÈÕÆÚ¸ñʽCHECK¸úÈÕÆڱȽÏ
  Ïêϸ½â¾ö·½°¸

JSÈÕÆÚ¸ñʽCHECK¸úÈÕÆڱȽÏ

Èȶȣº195   ·¢²¼Ê±¼ä£º2012-11-23 22:54:33.0
JSÈÕÆÚ¸ñʽCHECKºÍÈÕÆڱȽÏ
/* Check date format and validity */
function dateCheck(obj, labelName) {
var objName = eval(¡°document.all.¡± + obj);
var value = objName.value;
if (value == "" || value == null || value == undefined) {
return true;
}

if (value.match(/^\d{4}\/\d{2}\/\d{2}$ /) == null) {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
return false;
}

value = value.replace(/\//g, "");

if (numCheck(value) == false) {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
return false;
}

len = value.length;
if (len != {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
return false;

}

mon = parseIntX(value.substring(4, 6));
day = parseIntX(value.substring(6,);
switch(mon) {
case '1' :
case '3' :
case '5' :
case '7' :
case '8' :
case '10' :
case '12' :
if (day < 1 || day > 31) {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
return false;
}
break;
case '4' :
case '6' :
case '9' :
case '11' :
if (day < 1 || day >30) {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
return false;
}
break;
case '2' :
if (day < 1 || day > 29) {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
retrun false;
}
if (day == 29) {
year = parseInt(value.substring(0, 4));
if (year % 4 != 0) {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
retrun false;
}
if (year % 100 == 0) {
if (year % 400 != 0) {
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
retrun false;
}
}
}
break;
default :
alert(labelName + "¤Ë£Ù£Ù£Ù£Ù/£Í£Í/£Ä£ÄÐÎʽ¤òÈëÁ¦¤·¤Æ¤¯¤À¤µ¤¤¡£");
objName.focus();
retrun false;
}
return true;
}
/* Check number
* Used in function [dateCheck]
*/
function numCheck(value) {
len = value.length;
decPoint = false;
if (value == "-" || value == ".") {
return false;
}

for (i = 0; i < len; i++) {
c = value.charAt(i);
switch(c) {
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
break;
case '-' :
if (i != 0) {
return false;
}
break;
case '.' :
if (decPoint == true) {
return false;
}
decPoint = true;
break;
default :
return false;
}
}
return true;
}
/* Change date type to int.
* used in function [dateCheck]
*/
function parseIntX(value) {
str = allSpaceDel(value);
len = str.length;
for (i = 0; i < len; i++) {
c = str.charAt(i);
if (c != '0') {
break;
}
}
if (i >= len) {
i = i - 1;
}
if (i >= 0) {
return parseInt(str.substring(i, len));
} else {
return str;
}
}
/* Delete space.
* Used in function [parseIntX]
*/
function allSpaceDel(value) {
len = value.length;
str = "";
for (i = 0; i < len; i++) {
c = value.charAt(i);
if (c == ' ') {
} else {
str = str + c;
}
}
return str;
}
------------------------------------------------------------
/*
* Compare start date and end date, end date must later than start date.
*/
function dateCompare(strDate, endDate, labelName) {
var objName1 = eval("document.all." + strDate);
var objName2 = eval("document.all." + endDate);
var fromDate = objName1.value;
var toDate = objName2.value;

var objFromDate = new Date(fromDate.substring(0, 4)/1, fromDate.substring(5, 7)/1 - 1, fromDate.substring(8,10)/1);
var objToDate = new Date(toDate.substring(0, 4)/1, toDate.substring(5, 7)/1 - 1, toDate.substring(8,10)/1);
if (objFromDate > objToDate) {
alert(labelName + "¤Î½ËÁËÈÕ¤ËéßʼÈÕÒÔ½µ¤ÎÈÕ¸¶¤òÈëÁ¦¤·¤Æ¤¯¤À¤¤¡£");
objName2.focus();
return false;
}
return true;
}
------------------------------------------------------------
  Ïà¹Ø½â¾ö·½°¸