jsglobal.js

饶德元
2023-12-01
/**********************************************
* NAME:        JsGlobal.js              *******
* CONTENT:     in common use  function  *******
* AUTHOR:      mag                      *******
* DATE:        2002/12/21               *******
* @Return:   true :0;false:-1          *******
**********************************************/

//clear input and select with other one group redio 
function checkSelected(form, name, type)
{
	var elements = form.elements;
	var elementNum = elements.length;
 
	if (typeof(elements[name]) != "object")
	  	return false;
  
	if (typeof(elements[name][0]) != "object")
	{
		// only one item
		if(elements[name].type != type)
 			return false;  
 		else
 			return elements[name].checked;
 	}
    
 	if (elements[name][0].type != type)
  		return false;  
  
	for(var nLoop = 0; nLoop < elementNum; nLoop++){
  		if (typeof(elements[name][nLoop]) != "object")
			return false;
  
		if(elements[name][nLoop].checked == true){
			return true;
  		}
 	}
 	
 	return false;
}

//judge data is valid.
// use: ChangeDays(
//		document.forms[0].YearName,
//		document.forms[0].MonthName,
//		document.forms[0].DayName);
function changeDays(YearName,MonthName,DayName)
{
	var nYear = parseInt(YearName.options[YearName.selectedIndex].text,10);
	var nMonth = parseInt(MonthName.options[MonthName.selectedIndex].text,10);

	var nDays = checkDate(nYear, nMonth);
	
	if ((nDays < 1) || (nDays > 31)) return false;
		
	if (DayName.options.length < nDays)
	{
		// from day: 29 to 31
		DayName.options.length = nDays;
		DayName.options[nDays-3].text = nDays - 2;
		DayName.options[nDays-2].text = nDays - 1;
		DayName.options[nDays-1].text = nDays;
	}
	else
		DayName.options.length = nDays;

	return true;				
}

//judge data is valid.
function checkDate(nYear, nMonth)
{
	if ((nMonth < 1) || (nMonth > 12) || (nYear < 1990) || (nYear > 2100))
	{
		return -1;
	}
	else if (nMonth == 2)
	{
		if (((nYear % 4 == 0) && (nYear % 100 != 0)) || (nYear % 400 == 0))
		{
			return 28;
		}
		else
		{
			return 29;
			
		}
	}
	else if (nMonth == 4 || nMonth == 6 || nMonth == 9 || nMonth == 11)
	{
			return 30;
	}
	else
	{
			return 31;
	}
}

function consDate(year, mon, day)
{
	if(year == "----" && mon == "--" && day == "--")
	{
		return "999999999";
	}
	else if(year != "----" && mon != "--" && day != "--")
	{
		;
	}
	else 
	{
		alert("选定的日期不合法,请重新选定1!");
		return false;
	}
	year = new String(year);
	mon = new String(mon);
	day = new String(day);
	
	if(mon.length < 2) mon = "0" + mon; 
	if(day.length < 2) day = "0" + day; 

	year += mon + day;
	return year;
}

function SetDateFocus(power,YearName2,MonthName2,DayName2)
{
	if (power < -9900)
	{
		YearName2.focus();
	}
	else if (power < -99)
	{
		MonthName2.focus();
	}
	else
	{
		DayName2.focus();
	}
}

// date1 > date2
function CompareDate(YearName1,MonthName1,DayName1,YearName2,MonthName2,DayName2)
{
	var date1 = consDate(YearName1.options[YearName1.selectedIndex].text, MonthName1.options[MonthName1.selectedIndex].text, DayName1.options[DayName1.selectedIndex].text);
	var date2 = consDate(YearName2.options[YearName2.selectedIndex].text, MonthName2.options[MonthName2.selectedIndex].text, DayName2.options[DayName2.selectedIndex].text);
	if (!date1 || !date2)  return false;
	date2 -= date1;
	if (date2 > 0) return true;

	SetDateFocus(date2,YearName2,MonthName2,DayName2);

	alert("选定的日期不合法,请重新选定2!");
	return false;
}

// date1 >= date2
function CompareDate0(YearName1,MonthName1,DayName1,YearName2,MonthName2,DayName2)
{
	var date1 = consDate(YearName1.options[YearName1.selectedIndex].text, MonthName1.options[MonthName1.selectedIndex].text, DayName1.options[DayName1.selectedIndex].text);
	var date2 = consDate(YearName2.options[YearName2.selectedIndex].text, MonthName2.options[MonthName2.selectedIndex].text, DayName2.options[DayName2.selectedIndex].text);
	if (!date1 || !date2)  return false;
	date2 -= date1;
	if (date2 >= 0) return true;

	SetDateFocus(date2,YearName2,MonthName2,DayName2);

	alert("比较选定的日期不合法,请重新选定!");
	return false;
}

/*Check input character is number or not*/
function checkNum(checkStr)
{       
	range = "0123456789";
        var strCheck;
        for(var nLoop = 0; nLoop < checkStr.length; nLoop++){
	        strCheck = checkStr.substring(nLoop, nLoop + 1);
                if((range.indexOf(strCheck) == -1))return true;
        }
       return false;
}
 
/* check mail address: */
function checkEmail(strEMail) {
    var nlen,i;
    nlen = strEMail.length;
        
    if(strEMail.indexOf("@") == -1 || strEMail.indexOf(".") == -1 ||
    	strEMail.indexOf("@")!=strEMail.lastIndexOf("@"))
	return false;
		
    if (strEMail.charAt(0) == "@" || strEMail.charAt(0) == ".")
    return false;
     	
    if (strEMail.charAt(nlen-1) == "." )
    return false;
	     	
    if(strEMail.indexOf(".") < strEMail.indexOf("@") ||
	     strEMail.indexOf(".") == strEMail.indexOf("@") + 1)
    return false;
		           	
	return true;	
}

/* check Telephone */
function checkTel(strNum) {
    var i = 0;
    var ch = "";
    var varString = "0123456789-+";
 
    for (i = 0; i < strNum.length; i++) {
    	if( varString.indexOf(strNum.charAt(i))==-1 ) {
      		return false;
      	}
    }
    return true;
}

// trim \r,\n--ykw
function trim(strRough)
{
    var len = strRough.length;

    var nFirst, nEnd;
    var nFlag = 0;
    for(var nLoop = 0; nLoop < len; nLoop++)
    {
        if(strRough.charAt(nLoop) != " " && strRough.charAt(nLoop) != "\n" && strRough.charAt(nLoop) != "\r" && strRough.charAt(nLoop) != "\t")
        {
            nFlag++;
            if(nFlag == 1)nFirst = nLoop;
            nEnd = nLoop;
        }
    }
    var strTrim = strRough.substring(nFirst, nEnd + 1);
    return strTrim;
}
/*  check the Number is valid*/
function checkNumber(checkstr) 
{
	var checkok = "1234567890";
	checkstr = trimBlank(checkstr);
		
	for(i=0;i<checkstr.length;i++)
	{	
		str=checkstr.charAt(i);
		for (j = 0;  j < checkok.length;  j++)
			if (str == checkok.charAt(j))
			{
				break;
			}
		if (j == checkok.length )
		{
			alert("请输入正确的数字!");
			return  true;
		}
	}
	 return false;
}
 
/* Trim The String */
function trimBlank(str) {
    var i=0;    
    if(str.length==0)  {
        return '';
    }
    while(str.charAt(i) == ' ') {
        i++;
        if (i==str.Length) {
            return '';
        }
    }
    var j=str.length-1;
    while(str.charAt(j) == ' ') {
        j--;
    	if (j==0) {
    	return '';
        }
    }
    if (i>j) {
        return "";
    }
    return str.substring(i, j+1);
}

// check num is valid 
// use isvalidNumber(document.forms[0].textname)
function isValidNumber(checkstr)
{
	var ithreshold = parseInt(checkstr.value);
	if (checkNumber(checkstr.value))
		return false;
	else if (ithreshold < -2147483648 || ithreshold > 2147483647 )
	{
		alert('输入的值超出整形范围,请重新输入!');
		checkstr.focus();
		return false;
	}
	return true; 		
}

/* check the Length */
function getLength(strTmp)
{
	var len = 0;
    	if (strTmp == "") {
		len = 0;
		return len;    
    	}
    	
    	//differ  browser deal with
    	if (isNetscape()) 
    		return (strTmp.length);
    	else 
    	{
		for(var i = 0; i < strTmp.length; i++) {
	       	    	if ((strTmp.charCodeAt(i) < 128) || 
	       	    	(strTmp.charCodeAt(i) > 65376 && strTmp.charCodeAt(i) < 65440)) {
	           	 	len++;
	        	}
	        	else {
	            		len += 2;
	        	}
		}
	}
        return len;
}

/*judge charstring Length is valid */
// use document.forms[0].textname;
function isValidLength(checkstr)
{
	if (getLength(checkstr.value) > n)
	{
		alert('输入的字符超出指定范围,请重新输入!');
		checkstr.focus();
		return false;	
	}
	return true; 
}

function locate(URL)
{
	location = URL;
}

function isNetscape()
{
	return (navigator.appName.indexOf("Netscape")>=0);
}

function Action(strAct,descURL)
{
	document.forms[0].method.value = strAct;
	document.forms[0].action = descURL;
        document.forms[0].submit();
   	return true;
}

// funciton: list all elements within a form
// use as : Debug(document.form1);
function Debug(form)
{
	var objForm = form;
	var i;

	var msg = "Is this form named " + objForm.name;
	for(i=0; i<objForm.elements.length;i++)
	{
		msg += "\n\t" + objForm.elements[i].type + "\t\t" + objForm.elements[i].name;
	} 
	
	alert(msg);
	msg = "\n";
	for(i=0; i<objForm.all.length;i++)
	{
		msg += "\t" + objForm.all[i].type + "\t\t" + objForm.all[i].name;
	} 

	msg = "\n";
	for(i=0; i<objForm.attributes.length;i++)
	{
		msg += "\t" + objForm.attributes[i].type + "\t\t" + objForm.attributes[i].name;
	} 
}

 类似资料:

相关阅读

相关文章

相关问答