﻿
// ===============================================================================
//     javascript 输入校验函数库 ，用于通常 的数据输入校验

// ===============================================================================
function  gobackpage()
{
    history.go(-1);

}

function ickDate(obj)
{
    return true;
}

//javascript的trim函数，截取开头和结尾的空格
function  Trim(str)
{
    return  str.replace(/^\s*(.*?)[\s\n]*$/g,  '$1');
}
	
//校验普通电话、传真号码：可以“+”开头，除数字外，可含有“-”
function isFax(item)
{
    if(item.value==null || item.value=="")
	    return true;
    var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
    if (!patrn.exec(item.value))
    {
	    alert("请输入正确的传真号码！");
	    item.focus();
	    item.select();
	    return false;
    }
    return true;
}

//判断是否输入数字
function verifyAmt(item)//
{
	if(item.value==null || item.value=="")
	    return true;
	if (isNaN(item.value) )
	{
		alert("输入错误，只能输入数字.");
		item.focus();  
		item.select();
		return false;
	}
	return true;
}

//判断输入的是否是整数
function verifyInt(item)//
{
	if(item.value==null || item.value=="")
	    return true;
    var TelNo=item.value ;

    var iTel ;

	for  (iTel=0;iTel<TelNo.length;iTel++)
	{
		var s=TelNo.substring(iTel,iTel+1) ;
		if (isNaN(s) )
		{
            window.alert("输入错误,只能输入数字!");
		    item.focus();
		    item.select();
		    return false;
		    break ;
		}
	}
	return true;
}
    
//判断字符串长度是否不大于某个值,中文会当两个字来算
function IsValidLen(item,len)
{
    var str  = item.value;
    var strLen= str.length;
    var addLen= 0;
    
    for(var i=0;i<strLen;i++)
    {
        if(str.charCodeAt(i) > 128)
        {
            addLen++;
        }
    }

    if((str.length+addLen)>len)
    {
        alert("输入数据超过"+len+"个字符（" +(len/2) + "个中文文字），请重新输入！"); 
        item.value = str.substring(0,len);
		item.focus();
		item.select();
		return false;
    }
    else
    {
        return true;
    }
}

//判断输入的是否是电话号码
function verifyTel(item)//
{
	if(item.value==null || item.value=="")
	    return true;	
	if(item.value.length>12)
	{
	    IsValidLen(item,12);
	    item.focus();
	    item.select();	    
	    return false;
	}
    var patrn=/^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/;
    if (!patrn.exec(item.value))
    {
	    alert("请输入正确的电话号码！");
	    item.focus();
	    item.select();
	    return false;
    }
    return true;
}
	
//判断是否是有效的email地址
function isEmail(item) 
{    
    var i = 1;
    var s = item.value;
    var sLength = 0;
	if(s==null || s==""){
		return true;
	}
	else{
	    sLength = s.length;
	}
    while((i < sLength) && (s.charAt(i) != "@"))
    {
        i++;
    }
    if((i >= sLength) || (s.charAt(i) != "@"))
    {
        window.alert("无效的email地址");
	    item.focus();
	    item.select();
	    return false;
    }
    else i+=2;   
    while ((i < sLength) && (s.charAt(i) != "."))
    {
        i++;
    }

    if((i >= sLength - 1) || (s.charAt(i) != ".")) 
    {
        window.alert("无效的email地址");
	    item.focus();
	    item.select();
	    return false;
    }
    else 
        return true;
}

//判断日期是否输入正确
function checkday(year,month,day)
{
	
	if ( isNaN(year) || (year < 1900)) {
 
		return "年不合法";
	}
	if (  isNaN(month) || (month.length<2) || (month < 1) || (month > 12) ){
 
		return "月不合法";
	}
	if (  isNaN(day) || (day < 1) || (day > 31)){
 
		return "日不合法";
	}
	switch (parseInt(month)){
		case 2:
			high =28;
			if ((year % 4 == 0) && (year % 100 != 0))
				{high =29;}
			else if (year % 400 == 0) {high=29;}
			break;
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			high =31;
			break;
		default:
			high =30;
	}
	if ((day < 1) || (day > high)){
		return "日期不合法";
	}
	return "";
}

function CheckDate(item)  // 判断输入的日期是否有效
{   
	if(item.value==null || item.value=="")
	    return true;
    a_date=item.value ;
    
	a_date = a_date.replace(/\-/g,"/");

	var ayDate = a_date.split("/");
    
	if (ayDate.length != 3)
	{
	    window.alert("日期输入不正确");  
	    item.focus();
	    item.select();
	    return false;
    }
	var sY_M_D = checkday(ayDate[0],ayDate[1],ayDate[2]);
	if ( sY_M_D == "" )
		return true;
	var sY_D_M = checkday(ayDate[0],ayDate[2],ayDate[1]);
	if ( sY_D_M == "")
		return true;
     
    window.alert("日期输入不正确");  
    item.focus();
    item.select();
    return false;
}


 
 
function chkNull(nameStr)
{
    var i=0;
    if(nameStr == null || nameStr == "")
    {
        nameStr = "";
    }
    var arrName = nameStr.split(",");
    while(document.getElementsByTagName("INPUT")[i]!=null)
    {
        var j=0;
        while(arrName[j]!=null && arrName[j]!= "")
        {
            if(document.getElementsByTagName("INPUT")[i].name==arrName[j])
            {
                if(document.getElementsByTagName("INPUT")[i].value == null || document.getElementsByTagName("INPUT")[i].value == "")
                {
                    alert("带*的必填字段不能为空!");
                    return false;
                }                    
            }            
            j++;
        }
        i++;
    }
    return true;
}

//验证用户名和密码的长度和非法字符的过滤
function CheckLenAndStr(item,len)
{
    var errorStr="[~!@#$%^&*()+|<>\\/?]【～！◎＃￥％……※×（）——＋§《》？】『』{}÷＝。，、·`;:'；：”“‘’="+'"';
    var str  = item.value;
    var strLen= str.length;
    var addLen=0;
    
    for(var i=0;i<strLen;i++)
    {
       for(var j=0;j<errorStr.length;j++)
       {
           if(errorStr.charAt(j)==str.charAt(i))
           {
              alert('不能输入非法字符！');
              item.focus();
		      item.select();
              return false;
           }
       }
           
       if(str.charCodeAt(i) > 128)     //
       {
          addLen++;
       }
    }

    if((str.length+addLen)>len)
    {
        alert("输入数据超过"+len+"个字符（" +(len/2) + "个中文文字），请重新输入！"); 
        item.value = str.substring(0,len);
		item.focus();
		item.select();
		return false;
    }
    else
    {
        return true;
    }
}
