日期:2014-05-16  浏览次数:20440 次

js常用函数及客户端验证
在家没事的时候随便写的,呵呵~~~~

<html>
	<head>
		<script type="text/javascript">
		//判断两个时间的大小
			function timeCompare(){
				var startTime = document.getElementById("startTime").value;
				var endTime = document.getElementById("endTime").value;

				var arr1 = startTime.split("-");
				var date1 = new Date(arr1[0],arr1[1],arr1[2]);
				var dateTime = date1.getTime();
				
				var arr2 = endTime.split("-");
				var date2 = new Date(arr2[0],arr2[1],arr2[2]);
				var dateTime2 = date2.getTime();


				if(dateTime > dateTime2)
				{
					alert("开始时间大于结束时间");
				}
				else
				{
					alert("开始时间小于结束时间");
				}
			}
			//判断输入是否是整数
			function isInteger() 
			{ 
				var str = document.getElementById("txt").value;
				var regu = /^[-]{0,1}[0-9]{1,}$/; 
				alert(regu.test(str)); 
			}
			//判断邮箱熟人是否合法
			function isMail()
			{
				var mail = document.getElementById("mail").value;
				var reg = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/;
				//var reg = /^[-_A-Za-z0-9]+@([_A-Za-z0-9]+\.)+[A-Za-z0-9]{2,3}$/; 
				alert(reg.test(mail));
			}
			//电话号码验证
			function isMobel()
			{
				var phone = document.getElementById("phone").value;
				var reg = /^([0-9]+\-)+[0-9]{7}$/;
				alert(reg.test(phone));
			}
			
			//验证身份证号码
			function isIDno()  
			{   
				var strIDno = document.getElementById("iDnumber").value;
				var aCity={11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门",91:"国外"};  
			   
				var iSum = 0;  
				var info = "";  
				//var strIDno = obj.value;  
				var idCardLength = strIDno.length;    
				if(!/^\d{17}(\d|x)$/i.test(strIDno)&&!/^\d{15}$/i.test(strIDno))   
				{
					alert("非法身份证号");  
					return false;  
				}  
			   
				//在后面的运算中x相当于数字10,所以转换成a  
				strIDno = strIDno.replace(/x$/i,"a");  
			  
				if(aCity[parseInt(strIDno.substr(0,2))]==null)  
				{  
					alert("非法地区");  
					return false;  
				}  
				  
				if (idCardLength==18)  
				{  
					sBirthday=strIDno.substr(6,4)+"-"+Number(strIDno.substr(10,2))+"-"+Number(strIDno.substr(12,2));  
					var d = new Date(sBirthday.replace(/-/g,"/"))  
					if(sBirthday!=(d.getFullYear()+"-"+ (d.getMonth()+1) + "-" + d.getDate()))  
					{         
						alert("非法生日");  
						return false;  
					}  
			  
					for(var i = 17;i>=0;i --)  
						iSum += (Math.pow(2,i) % 11) * parseInt(strIDno.charAt(17 - i),11);  
			  
					if(iSum%11!=1)  
					{  
						alert("非法身份证号");  
						return false;  
					}  
				}  
				else if (idCardLength==15)  
				{  
					sBirthday = "19" + strIDno.substr(6,2) + "-" + Number(strIDno.substr(8,2)) + "-" + Number(strIDno.substr(10,2));  
					var d = new Date(sBirthday.replace(/-/g,"/"))  
					var dd = d.getFullYear().toString() + "-" + (d.getMonth()+1) + "-" + d.getDate();     
					if(sBirthday != dd)  
					{  
						alert("非法生日");  
						return false;  
					}  
				}  
				alert("身份证合法");
				return true;
			}

			//验证时间是否合法 12:23:11 格式
			function isTimeValidate()
			{
				var time = document.getElementById("time").value;
				var reg = /^[0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}:[0-9]{1}[0-9]{1}$/;
				if(!reg.test(time))
				{
					alert("时间输入不合法");
					return;
				}
				var hour = time.substring(0,2);
				var minute = time.substring(3,5);
				var second =  time.substring(6);
				if(hour>23 || hour < 0 )
				{
					alert("时间输入不合法");
					return;
				}
				if(minute >59 || minute < 0)
				{
					alert("时间输入不合法");
					return;
				}
				if(second >59 || second < 0 )
				{
					alert("时间输入不合法");
					return;
				}
			}
			//验证是否是整数
			function isNumber()
			{
				var number = document.getElementById("Number").value;
				var reg = /^\d+$/;
				alert(reg.test(number));
			}

			//验证是否是汉字
			function