首页 > 网站 > WEB开发 > 正文

JAVASCRIPT校验大全[转]

2024-04-27 14:17:21
字体:
来源:转载
供稿:网友

javaSCRipT校验大全[转]

varIsFirefox=document.getElementById&&!document.all;//判断是否为FireFox//页面里回车到下一控件的焦点functionEnter2Tab(e){try{varob=IsFireFox?e.target:event.srcElement;if(ob.tagName=="INPUT"&&(ob.type=="text"||ob.type=="passWord"||ob.type=="checkbox"||ob.type=="radio")||ob.tagName=="SELECT"){varkey=IsFireFox?e.which:event.keyCode;if(key==13){if(IsFireFox){event.which=9;}else{event.keyCode=9;}}}}catch(E){}}//打开此功能请取消下行注释//document.onkeydown=Enter2Tab;//Ajax对象创建functionAjaxLib(){varobjxmlHttp;if(window.xmlhttpRequest){objXMLHttp=newXMLHttPRequest();}else{//使用该代码,出现问题varMSXML=[’MSXML2.XMLHTTP.5.0’,’MSXML2.XMLHTTP.4.0’,’MSXML2.XMLHTTP.3.0’,’MSXML2.XMLHTTP’,’Microsoft.XMLHTTP’];varMSXML=[’MSXML2.XMLHTTP’,’Microsoft.XMLHTTP’];for(varn=0;n<MSXML.length;n++){try{objXMLHttp=newActiveXObject(MSXML[n]);break;}catch(e){}}if(!objXMLHttp&&typeofXMLHttpRequest!=’undefined’){objXMLHttp=newXMLHttpRequest();}}returnobjXMLHttp;}//判断用户名是否符合标准functionUserNameIsOk(ssn){varre=/^[0-9a-z][w-.]*[0-9a-z]$/i;returnre.test(ssn)?true:false;}//_______全选择__________functionSelectAll(){varempty;varf=document.forms[0];for(vari=0;i<f.length;i++){empty=f[i];if(empty.type=="checkbox"&&empty.disabled==false)empty.checked=true;}}//__________返选择_________functionSelectReverse(){varempty;varf=document.forms[0];for(vari=0;i<f.length;i++){empty=f[i];if(empty.type=="checkbox"&&empty.disabled==false)if(empty.checked==true){empty.checked=false;}else{empty.checked=true;}}}//提交查询内容submitsearchfindcontentfunctionGoPage(pn){try{document.forms[0].page.value=pn;document.forms[0].submit();}catch(E){document.forms[1].page.value=pn;document.forms[1].submit();}}functionWoodsAlert(ob,tip){alert(tip);//ob.value="";ob.focus();}//__________处理特殊字符________________functioninvalid_keyword(){varkeyword=newArray("<",">","%","’",""");varf,ob;for(varform=0;form<document.forms.length;form++){f=document.forms[form];for(vari=0;i<f.length;i++){ob=f[i];if(ob.type=="text"&&ob.disabled==false){for(j=0;j<keyword.length;j++){if(ob.value.indexOf(keyword[j])!=-1){alert("在输入框聚焦处发现非法字符["+keyword[j]+"]。");//ob.value="";ob.focus();returnfalse;}}ob.value=Trim(ob.value);}}}returntrue;}functionIsTime(str)//__________判断是否为时间字符串________________________{vara=str.match(/^(d{1,2})(:)?(d{1,2})2(d{1,2})$/);if(a==null){alert(’输入的参数不是时间格式’);returnfalse;}if(a[1]>24||a[3]>60||a[4]>60){returnfalse}returntrue;}functionIsDate(str)//_________是否为日期(YYYY-MM-DD)类型字符串_________________________{varr=str.match(/^(d{1,4})(-|/)(d{1,2})2(d{1,2})$/);if(r==null)returnfalse;vard=newDate(r[1],r[3]-1,r[4]);return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]);}functionIsBigCurrentDate(str)//_________________判断用户给出的日期是否大于当前日期______________________________{vartoday=newDate();vartY=String(today.getFullYear());vartM=String(today.getMonth()+1);(tM.length<=1)?tM="0"+tM:tM=tM;vartD=String(today.getDate());(tD.length<=1)?tD="0"+tD:tD=tD;vartH=String(today.getHours());(tH.length<=1)?tH="0"+tH:tH=tH;vartM1=String(today.getMinutes());(tM1.length<=1)?tM1="0"+tM1:tM1=tM1;vartdate=tY+"-"+tM+"-"+tD+""+tH+":"+tM1+":59";//zhuweisong添加秒的叛断if(str>tdate){returntrue;}else{returnfalse;}}functionIsDateTime(str)//____________是否为时间日期类型__________________________________{varreg=/^(d{1,4})(-|/)(d{1,2})2(d{1,2})(d{1,2}):(d{1,2}):(d{1,2})$/;varr=str.match(reg);if(r==null)returnfalse;vard=newDate(r[1],r[3]-1,r[4],r[5],r[6],r[7]);return(d.getFullYear()==r[1]&&(d.getMonth()+1)==r[3]&&d.getDate()==r[4]&&d.getHours()==r[5]&&d.getMinutes()==r[6]&&d.getSeconds()==r[7]);}functionIsEmail(mail)//_____________判断是否是有效的EMAIL地址________________________________{return(newRegExp(/^w+((-w+)|(.w+))*@[A-Za-z0-9]+((.|-)[A-Za-z0-9]+)*.[A-Za-z0-9]+$/).test(mail));}functionIsIp(s)//________________判断是否为有效IP地址_________________________________{varcheck=function(v){try{return(v<=255&&v>=0);}catch(x){returnfalse;}}varre=s.split(".")return(re.length==4)?(check(re[0])&&check(re[1])&&check(re[2])&&check(re[3])):false}//_________判断输入的是否为汉字___________begin__________functionisCharsInBag(s,bag){vari,c;for(i=0;i<s.length;i++){c=s.charAt(i);//字符串s中的字符if(bag.indexOf(c)>-1)returnc;}return"";}functionIsCn(s){varerrorChar;varbadChar="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789><,[]{}?/+=|’":;~!#$%()`";errorChar=isCharsInBag(s,badChar)if(errorChar!=""){returnfalse;}returntrue;}//_________判断输入的是否为汉字___________end__________//________函数功能:判断输入的字符串是不是英文__________begin___________functionisCharsInBagEn(s,bag){vari,c;for(i=0;i<s.length;i++){c=s.charAt(i);//字符串s中的字符if(bag.indexOf(c)<0)returnc;}return"";}functionIsEn(s){varerrorChar;varbadChar="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";errorChar=isCharsInBagEn(s,badChar)return(errorChar!="")?false:true;}//________函数功能:判断输入的字符串是不是英文__________end___________/**//********************函数名称:IsTelephone函数功能:固话,手机号码检查函数,合法返回true,反之,返回false函数参数:obj,待检查的号码检查规则:  (1)电话号码由数字、"("、")"和"-"构成  (2)电话号码为3到8位  (3)如果电话号码中包含有区号,那么区号为三位或四位  (4)区号用"("、")"或"-"和其他部分隔开  (5)移动电话号码为11或12位,如果为12位,那么第一位为0  (6)11位移动电话号码的第一位和第二位为"13"  (7)12位移动电话号码的第二位和第三位为"13"********************/functionIsTelephone(obj){varpattern=/(^([0+]d{2,3})d{3,4}-d{3,8}$)|(^([0+]d{2,3})d{3,4}d{3,8}$)|(^([0+]d{2,3}){0,1}13d{9}$)|(^d{3,4}d{3,8}$)|(^d{3,4}-d{3,8}$)/;return(pattern.test(obj))?true:false;}/**//********************函数名称:IsLegality函数功能:检查字符串的合法性,即是否包含"’字符,包含则返回false;反之返回true函数参数:obj,需要检测的字符串********************/functionIsLegality(obj){varintCount1=obj.indexOf(""",0);varintCount2=obj.indexOf("’",0);return(intCount1>0||intCount2>0)?false:true;}/**//********************函数名称:IsNumber函数功能:检测字符串是否全为数字函数参数:str,需要检测的字符串********************/functionIsNumber(str){varnumber_chars="1234567890";vari;for(i=0;i<str.length;i++){if(number_chars.indexOf(str.charAt(i))==-1)returnfalse;}returntrue;}/**//********************函数名称:Trim函数功能:去除字符串两边的空格函数参数:str,需要处理的字符串********************/functionTrim(str){returnstr.replace(/(^s*)|(s*$)/g,"");}/**//********************函数名称:LTrim函数功能:去除左边的空格函数参数:str,需要处理的字符串********************/functionLTrim(str){returnstr.replace(/(^s*)/g,"");}/**//********************函数名称:RTrim函数功能:去除右边的空格函数参数:str,需要处理的字符串********************/functionRTrim(str){returnthis.replace(/(s*$)/g,"");}/**//********************函数名称:IsNull函数功能:判断给定字符串是否为空函数参数:str,需要处理的字符串********************/functionIsNull(str){return(Trim(str)=="")?false:true;}//获取cookiefunctiongetCookie(name){varsearch=name+"="if(document.cookie.length>0){offset=document.cookie.indexOf(search)if(offset!=-1){offset+=search.lengthend=document.cookie.indexOf(";",offset)if(end==-1)end=document.cookie.lengthreturnunescape(document.cookie.substring(offset,end))}elsereturn"";}}//存储cookiefunctionsetCookie(name,value,days){if(days){vardate=newDate();date.setTime(date.getTime()+(days*24*60*60*1000));varexpires=";expires="+date.toGMTString();}elsevarexpires="";document.cookie=name+"="+value+expires+";path=/;domain="+document.location.hostname;}functioneventFalse(){window.event.returnValue=false;}//document.oncontextmenu=eventFalse;//document.onselectstart=eventFalse;//-->


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表