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

记录一些常用的JavaScript的方法

1.?检查输入IP:

function checkIP()
{
	var ipArray,ip,j;
	ip = document.ipform.ip.value;
 
	if (ip.indexOf(" ")>=0){
		ip = ip.replace(/ /g,"");
		document.ipform.ip.value = ip;
	}
	if (ip.toLowerCase().indexOf("http://")==0){
		ip = ip.slice(7);
		document.ipform.ip.value = ip;
	}
	if (ip.toLowerCase().indexOf("https://")==0){
		ip = ip.slice(8);
		document.ipform.ip.value = ip;
	}
	if (ip.slice(ip.length-1)=="/"){
		ip = ip.slice(0,ip.length-1);
		document.ipform.ip.value = ip;
	}
 
	if(/[A-Za-z_-]/.test(ip)){
	if(!/^([\w-]+\.)+((com)|(net)|(org)|(gov\.cn)|(info)|(cc)|(com\.cn)|(net\.cn)|(org\.cn)|(name)|(biz)|(tv)|(cn)|(mobi)|(name)|(sh)|(ac)|(io)|(tw)|(com\.tw)|(hk)|(com\.hk)|(ws)|(travel)|(us)|(tm)|(la)|(me\.uk)|(org\.uk)|(ltd\.uk)|(plc\.uk)|(in)|(eu)|(it)|(jp)|(co)|(me)|(mx)|(ca)|(ag)|(com\.co)|(net\.co)|(nom\.co)|(com\.ag)|(net\.ag)|(fr)|(org\.ag)|(am)|(asia)|(at)|(be)|(bz)|(com\.bz)|(net\.bz)|(net\.br)|(com\.br)|(de)|(es)|(com\.es)|(nom\.es)|(org\.es)|(fm)|(gs)|(co\.in)|(firm\.in)|(gen\.in)|(ind\.in)|(net\.in)|(org\.in)|(jobs)|(ms)|(com\.mx)|(nl)|(nu)|(co\.nz)|(net\.nz)|(org\.nz)|(tc)|(tk)|(org\.tw)|(idv\.tw)|(co\.uk)|(vg)|(ad)|(ae)|(af)|(ai)|(al)|(an)|(ao)|(aq)|(ar)|(as)|(au)|(aw)|(az)|(ba)|(bb)|(bd)|(bf)|(bg)|(bh)|(bi)|(bj)|(bm)|(bn)|(bo)|(br)|(bs)|(bt)|(bv)|(bw)|(by)|(cd)|(cf)|(cg)|(ch)|(ci)|(ck)|(cl)|(cm)|(cr)|(cu)|(cv)|(cx)|(cy)|(cz)|(dj)|(dk)|(dm)|(do)|(dz)|(ec)|(ee)|(eg)|(er)|(et)|(fi)|(fj)|(fk)|(fo)|(ga)|(gd)|(ge)|(gf)|(gg)|(gh)|(gi)|(gl)|(gm)|(gn)|(gp)|(gq)|(gr)|(gt)|(gu)|(gw)|(gy)|(hm)|(hn)|(hr)|(ht)|(hu)|(id)|(ie)|(il)|(im)|(iq)|(ir)|(is)|(je)|(jm)|(jo)|(ke)|(kg)|(kh)|(ki)|(km)|(kn)|(kr)|(kw)|(ky)|(kz)|(lb)|(lc)|(li)|(lk)|(lr)|(ls)|(lt)|(lu)|(lv)|(ly)|(ma)|(mc)|(md)|(mg)|(mh)|(mk)|(ml)|(mm)|(mn)|(mo)|(mp)|(mq)|(mr)|(mt)|(mu)|(mv)|(mw)|(my)|(mz)|(na)|(nc)|(ne)|(nf)|(ng)|(ni)|(no)|(np)|(nr)|(nz)|(om)|(pa)|(pe)|(pf)|(pg)|(ph)|(pk)|(pl)|(pm)|(pn)|(pr)|(ps)|(pt)|(pw)|(py)|(qa)|(re)|(ro)|(ru)|(rw)|(sa)|(sb)|(sc)|(sd)|(se)|(sg)|(si)|(sk)|(sl)|(sm)|(sn)|(sr)|(st)|(sv)|(sy)|(sz)|(td)|(tf)|(tg)|(th)|(tj)|(tl)|(tn)|(to)|(tr)|(tt)|(tz)|(ua)|(ug)|(uk)|(uy)|(uz)|(va)|(vc)|(ve)|(vi)|(vn)|(vu)|(wf)|(ye)|(yt)|(yu)|(za)|(zm)|(zw))$/.test(ip)){
			alert("不是正确的域名");
			document.ipform.ip.focus();
			return false;
		}
	}
	else{
		ipArray = ip.split(".");
		j = ipArray.length
		if(j!=4)
		{
			alert("不是正确的IP");
			document.ipform.ip.focus();
			return false;
		}
 
		for(var i=0;i<4;i++)
		{
			if(ipArray[i].length==0 || ipArray[i]>255)
			{
				alert("不是正确的IP");
				document.ipform.ip.focus();
				return false;
			}
		}
	}
}

?2.检查输入手机号码:

function checkMobile(){
	var sMobile = document.mobileform.mobile.value
	if(!(/^1[3|4|5|8][0-9]\d{4,8}$/.test(sMobile))){
		alert("不是完整的11位手机号或者正确的手机号前七位");
		document.mobileform.mobile.focus();
		return false;
	}
}

3.利用正则验证邮箱:

?

function checkEmail() {
	var regEmail = "/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/";
	if (!regEmail.test(email)) {
		alert("邮件格式错误");
	}
}

4.利用正则验证用户名:?

function checkUsername(){
	var regEmail = "/^[a-zA-Z]{1}\w+$/";
	if (!regEmail.test(email)) {
		alert("用户名格式错误");
	}
}
?5.检查其他的:
function checkZip(){
	var sZip = document.zipform.zip.value
	if(!(/^\d{4,6}$/.test(sZip))){
		alert("请输入邮政编码前4-6位");
		return false;
	}
}
 
function checkZone(){
	var sZone = document.zoneform.zone.value
	if(!(/^0\d{2,6}$/.test(sZone))){
		alert("请输入以“0”开头的3-7位区号");
		return false;
	}
}
 
function checkArea2Zip(){
	var sArea = document.area2zipForm.area.value
	if(sArea==""){
		alert("请输入地址");
		document.area2zipForm.area.focus();
		return false;
	}
	if(sArea.length<2){
		alert("地址至少要有2个字");
		document.area2zipForm.area.focus();
		return false;
	}
}
 
function checkArea2Zone(){
	var sArea = document.area2zoneForm.area.value
	if(sArea==""){
		alert("请输入地址");
		do