从阿里妈妈发现的几个不错的表单验证函数
2018-09-21 18:56
//--------
//检查当前浏览器是否为Netscape
//--------
functionisNetscape(){
app=navigator.appName.substring(0,1);
if(app==N)returntrue;
else{returnfalse;}
}
//--------
//保存当前Form表单(仅适用于IE浏览器)
//--------
functionformSaveCheck(fileName){
if(isNetscape()){alert(Sorry,thesefunctionisnotsupported)}
elsedocument.execCommand(SaveAs,null,fileName)
}
//--------
//校验数据的合法性
//--------
functionisValidReg(chars){
varre=/<>\[\]\{\}『』※○●◎§△▲☆★◇◆□▼㊣﹋⊕⊙〒ㄅㄆ
ㄇㄈㄉㄊㄋㄌㄍㄎㄏㄐㄑㄒㄔㄕㄖㄗㄘㄙㄚㄛㄜㄝㄞㄟㄢㄣㄤㄥㄦㄧㄨㄩ
■▄▆\*@#\^\\/;
if(re.test(chars)==true){
returnfalse;
}else{
returntrue;
}
}
//--------
//检查数据的长度是否合法
//--------
functionisValidLength(chars,len){
if(chars.length>len){
returnfalse;
}
returntrue;
}
//--------
//校验URL的合法性
//--------
functionisValidURL(chars){
//varre=/^([hH][tT]{2}[pP]:\/\/[hH][tT]{2}[pP][sS]:\/\/)((((\w+(-*\w*)+)\.)+((com)
(net)(edu)(gov)(org)(biz)(aero)(coop)(info)(name)(pro)(museum))(\.([a-z]{2}))?)((\w+(-
*\w*)+)\.(cn)))$/;
varre=/^([hH][tT]{2}[pP]:\/\/[hH][tT]{2}[pP][sS]:\/\/)(\S+\.\S+)$/;
//varre=/^([hH][tT]{2}[pP]:\/\/[hH][tT]{2}[pP][sS]:\/\/)(((((\w+(-*\w*)+)\.)+((com)
(net)(edu)(gov)(org)(biz)(aero)(coop)(info)(name)(pro)(museum)(cn)(tv)(hk))(\.([a-z]
{2}))?)((\w+(-*\w*)+)\.(cn)))((\/\?)\S*)*)$/;
if(!isNULL(chars)){
chars=jsTrim(chars);
if(chars.match(re)==null)
returnfalse;
else
returntrue;
}
returnfalse;
}
//--------
//校验数字的合法性
//--------
functionisValidDecimal(chars){
varre=/^\d*\.?\d{1,2}$/;
if(chars.match(re)==null)
returnfalse;
else
returntrue;
}
//--------
//校验数字的合法性
//--------
functionisNumber(chars){
varre=/^\d*$/;
if(chars.match(re)==null)
returnfalse;
else
returntrue;
}
//--------
//校验邮编的合法性
//--------
functionisValidPost(chars){
varre=/^\d{6}$/;
if(chars.match(re)==null)
returnfalse;
else
returntrue;
}
//--------
//去掉数据的首尾空字符
//--------
functionjsTrim(value){
returnvalue.replace(/(^\s*)(\s*$)/g,);
}
//--------
//校验数据是否为空(当数据为空字符时也为NULL)
//--------
functionisNULL(chars){
if(chars==null)
returntrue;
if(jsTrim(chars).length==0)
returntrue;
returnfalse;
}
//--------
//校验Email的合法性
//--------
functioncheckEmail(fieldName,bMsg)
{
varemailStr=fieldName.value;
varemailPat=/^(.+)@(.+)$/
varspecialChars=\\(\\)<>@,;:\\\\\\\\\.\\[\\]
varvalidChars=\[^\\s+specialChars+\]
varquotedUser=(\[^\]*\)
varipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
varatom=validChars++
varword=(+atom++quotedUser+)
varuserPat=newRegExp(^+word+(\\.+word+)*$)
vardomainPat=newRegExp(^+atom+(\\.+atom+)*$)
varmatchArray=emailStr.match(emailPat)
if(matchArray==null)
{
if(bMsg)alert(Emailaddressseemsincorrect(check@and.s))
returnfalse
}
varuser=matchArray[1]
vardomain=matchArray[2]
//Seeifuserisvalid
if(user.match(userPat)==null)
{
if(bMsg)alert(TheEmailaddressseemsincorrect.)
//fieldName.focus();
returnfalse
}
/*ifthee-mailaddressisatanIPaddress(asopposedtoasymbolic
hostname)makesuretheIPaddressisvalid.*/
varIPArray=domain.match(ipDomainPat)
if(IPArray!=null)
{
for(vari=1;i<=4;i++)
{
if(IPArray[i]>255)
{
if(bMsg)alert(DestinationIPaddressisinvalid!)
returnfalse
}
}
returntrue
}
//Domainissymbolicname
vardomainArray=domain.match(domainPat)
if(domainArray==null)
{
if(bMsg)alert(Thedomainnamedoesntseemtobevalid.)
returnfalse
}
/*domainnameseemsvalid,butnowmakesurethatitendsina
three-letterword(likecom,edu,gov)oratwo-letterword,
representingcountry(uk,nl),andthattheresahostnamepreceding
thedomainorcountry.*/
varatomPat=newRegExp(atom,g)
vardomArr=domain.match(atomPat)
varlen=domArr.length
if(domArr[domArr.length-1].length<2domArr[domArr.length-1].length>3)
{
//theaddressmustendinatwoletterorthreeletterword.
if(bMsg)alert(Theaddressmustendinathree-letterdomain,ortwolettercountry.)
returnfalse
}
//Makesuretheresahostnameprecedingthedomain.
if(len<2)
{
if(bMsg)alert(Thisaddressismissingahostname!)
returnfalse
}
//Ifwevegotthisfar,everythingsvalid!
returntrue;
}
//--------
//判断是否为闰年
//--------
functionisLeapYear(year){
if(year%4!=0)
returnfalse;
if(year%400==0)
returntrue;
if(year%100==0)
returnfalse;
returntrue;
}
//--------
//校验日期的合法性
//--------
functionvalidateDate(day,month,year)
{
if((day<=0)(month<=0)(year<=0))
returnfalse;
if((month>=1)&&(month<=12)){
if(month==2){
if(isLeapYear(year)){
if(day<=29)
returntrue;
}else{
if(day<=28)
returntrue;
else
returnfalse;
}
}elseif((month==4)(month==6)(month==9)(month==11)){
if(day<=30)
returntrue;
else
returnfalse;
}else{
if(day<=31)
returntrue;
else
returnfalse;
}
}
returnfalse;
}
//--------
//判断数据是否包含都是SingleByte
//--------
functionisSingleByteString(str)
{
varrc=true;
varj=0,i=0;
for(i=0;i<str.length;i++){
j=str.charCodeAt(i);
if(j>=128){
rc=false;
break;
}
}
returnrc;
}
varsubmitEvent=true;
functioncheckDoubleSubmit(){
returnsubmitEvent;
}
//--------
//弹出窗口
//参数:url-弹出窗口显示URL的内容
//w-弹出窗口的宽度
//h-弹出窗口的高度
//isCenter-控制弹出窗口是否在屏幕中央显示,值为true/false
//isResizable-控制弹出窗口是否可以改变大小,值为true/false
//isScroll-控制弹出窗口是否有滚动条,值为true/false
//--------
functionpopupWindow(url,w,h,isCenter,isResizable,isScroll){
if(isNULL(url))return;
varscrLeft=0;
varscrTop=0;
varscroll=no;
varresize=no;
if(isCenter){
scrLeft=(screen.width-w)/2;
scrTop=(screen.height-h)/2;
}
if(isResizable)resize=yes;
if(isScroll)scroll=yes;
window.open(url,popupWindow,
height=+h+,width=+w+,top=+scrTop+,left=+scrLeft+,toolbar=no,menubar=no,scrollbars=+scrol
l+,resizable=+resize+,location=no,status=no);
}
//--------
//弹出窗口
//参数:url-弹出窗口显示URL的内容
//w-弹出窗口的宽度
//h-弹出窗口的高度
//isCenter-控制弹出窗口是否在屏幕中央显示,值为true/false
//isResizable-控制弹出窗口是否可以改变大小,值为true/false
//isModal-控制弹出窗口是否为模式或非模式对话框,值为ture/false
//--------
functionpopupModalWindow(url,w,h,isCenter,isResizable,isModal){
if(isNULL(url))return;
varscrLeft=0;
varscrTop=0;
varresize=no;
varcnt=no;
if(isCenter){
cnt=yes;
scrLeft=(screen.width-w)/2;
scrTop=(screen.height-h)/2;
}
if(isResizable)resize=yes;
if(isModal)
window.showModalDialog(url,popupWindow,
dialogWidth:+w+px;dialogHeight:+h+px;dialogLeft:+scrLeft+px;dialogTop:+scrTop+px;center:
+cnt+;help:no;resizable:+resize+;status:no);
else
window.showModelessDialog(url,popupWindow,
dialogWidth:+w+px;dialogHeight:+h+px;dialogLeft:+scrLeft+px;dialogTop:+scrTop+px;center:
+cnt+;help:no;resizable:+resize+;status:no);
}
//--------
//弹出窗口
//参数:url-弹出窗口显示URL的内容
//w-弹出窗口的宽度
//h-弹出窗口的高度
//isCenter-控制弹出窗口是否在屏幕中央显示,值为true/false
//isResizable-控制弹出窗口是否可以改变大小,值为true/false
//isScroll-控制弹出窗口是否有滚动条,值为true/false
//--------
functionopenWindowCenter(urll,w,h){
vartop=(window.screen.height-h)/2;
varleft=(window.screen.width-w)/2;
varparam=toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no,top=;
param=param+top;
param=param+,left=;
param=param+left;
param=param+,height=+h;
param=param+,width=+w;
varw=window.open(urll,,param)
if(w!=null&&typeof(w)!=undefined){
w.focus();
}
}
<scriptlanguage=javascript>
functionencodeHtmlForText(text){
text=text.replace(newRegExp(&,gm),);
text=text.replace(newRegExp(<,gm),<);
text=text.replace(newRegExp(>,gm),>);
text=text.replace(newRegExp(\,gm),');
text=text.replace(newRegExp(\,gm),");
returntext;
}
functionencodeHtmlForURL(url){
url=url.replace(newRegExp(<,gm),<);
url=url.replace(newRegExp(>,gm),>);
url=url.replace(newRegExp(\,gm),');
url=url.replace(newRegExp(\,gm),");
returnurl;
}
functiontitle_url_change(oipt){
if(oipt){
varcnt=oipt.value.length;
if(cnt<17){
}else{
$i(cntTitle).style.color=red;
$i(cntTitle).innerHTML=(文本超长!您已输入+cnt+字);
}
}
vartitle=$i(_fmm.t._0.ti).value;
varclickurl=$i(_fmm.t._0.cl).value;
title=encodeHtmlForText(title);
clickurl=encodeHtmlForURL(clickurl);
$i(adTitle).innerHTML=<ahref=+clickurl+>+title+</a>;
}
functiontitle_change(){
vartitle=$i(_fmm.t._0.ti).value;
title=encodeHtmlForText(title);
$i(adTitle).innerHTML=title;
}
functiondescription_change(oipt){
if(oipt){
varcnt=oipt.value.length;
if(cnt<33){
$i(cntDesc).style.color=blue;
$i(cntDesc).innerHTML=(您已输入+cnt+字);
}else{
$i(cntDesc).style.color=red;
$i(cntDesc).innerHTML=(文本超长!您已输入+cnt+字);
}
}
vardesc=$i(_fmm.t._0.d).value;
desc=encodeHtmlForText(desc);
$i(adDesc).innerHTML=desc;
}
functiondispurl_change(){
vardispurl=$i(_fmm.t._0.di).value;
dispurl=encodeHtmlForURL(dispurl);
$i(adDispUrl).innerHTML=dispurl;
}
functionlinkurl_change(){
varclickurl=$i(_fmm.t._0.cl).value;
clickurl=encodeHtmlForURL(clickurl);
$i(adClickUrl).innerHTML=clickurl;
}
functioncheck_count($str){
if(!$str)return0;
var$c=0;
for(var$i=0;$i<$str.length;$i++){
if($str.charCodeAt($i)>255){
$c++;
$c++;
}else
$c++;
}
return$c;
}
functionvalidation(){
$i(_fmm.t._0.n).value=jsTrim($i(_fmm.t._0.n).value);
$i(_fmm.t._0.ti).value=jsTrim($i(_fmm.t._0.ti).value);
$i(_fmm.t._0.d).value=jsTrim($i(_fmm.t._0.d).value);
$i(_fmm.t._0.di).value=jsTrim($i(_fmm.t._0.di).value);
$i(_fmm.t._0.cl).value=jsTrim($i(_fmm.t._0.cl).value);
varadName=$i(_fmm.t._0.n).value;
varadtitle=$i(_fmm.t._0.ti).value;
varaddescription=$i(_fmm.t._0.d).value;
varaddispurl=$i(_fmm.t._0.di).value;
varadclickurl=$i(_fmm.t._0.cl).value;
varmsg=;
$i(_fmm.t._0.t).value=jsTrim($i(_fmm.t._0.t).value);
vartags=$i(_fmm.t._0.t).value;
if(!isValidReg(tags)){
msg=msg+关键词包含非法字符!\n;
}
if(!isValidLength(tags,32)){
msg=msg+您输入的关键词长度超过限制!\n
}
if(isNULL(adName)){
msg=msg+请输入广告名称!\n;
}
if(!isValidReg(adName)){
msg=msg+广告名称包含非法字符!\n;
}
if(!isValidLength(adName,16)){
msg=msg+输入广告名称长度超过限制!\n
}
if(isNULL(adtitle)){
msg=msg+请输入标题内容!\n;
}
if(!isValidLength(adtitle,16)){
msg=msg+输入标题长度超过限制!\n;
}
if(!isValidReg(adtitle)){
msg=msg+标题内容包含非法字符!\n;
}
if(isNULL(addescription)){
msg=msg+请输入描述内容!\n;
}
if(!isValidLength(addescription,32)){
msg=msg+输入描述长度超过限制!\n;
}
if(!isValidReg(addescription)){
msg=msg+描述内容包含非法字符!\n;
}
if(isNULL(addispurl)){
msg=msg+请输入显示网址内容!\n;
}
if(!isValidLength(addispurl,1024)){
msg=msg+输入显示网址长度超过限制!\n;
}
if(!isValidURL(addispurl)){
msg=msg+显示网址输入不合法!\n;
}
if(isNULL(adclickurl)){
msg=msg+请输入链接网址内容!\n;
}
if(!isValidLength(adclickurl,512)){
msg=msg+输入链接网址长度超过限制!\n;
}
if(!isValidURL(adclickurl)){
msg=msg+链接网址输入不合法!\n;
}
if(getHostName(adclickurl)!=getHostName(addispurl)){
msg=msg+显示URL必须与链接URL位于同一域名下\n;
}
if(!isNULL(msg)){
alert(msg);
returnfalse;
}
returntrue;
}
functiongetHostName(a){
a=a.toLowerCase();
varheadlength=0;
if(a.indexOf(
headlength=7;
}elseif(a.indexOf(
headlength=8;
}
a=a.substr(headlength);
if(a.indexOf(/)>-1){
a=a.substr(0,a.indexOf(/));
}
returna;
}
functionaddConfirm(form){
varisvalidate=validation();
if(isvalidate){
form.submit();
}else{
returnfalse;
}
}
functiondoTest(){
varadclickurl=$i(_fmm.t._0.cl).value;
varmsg=;
if(isNULL(adclickurl)){
msg=msg+请输入链接网址内容!\n;
}
if(!isValidLength(adclickurl,512)){
msg=msg+输入链接网址长度超过限制!\n;
}
if(!isValidURL(adclickurl)){
msg=msg+链接网址输入不合法!\n;
}
if(!isNULL(msg)){
alert(msg);
returnfalse;
}
popup=window.open(adclickurl,popupnav,
width=400,height=300,left=480,top=250,resizable=1,scrollbars=yes);
returnfalse;
}
functiondoInit(){
vardispURL=$i(_fmm.t._0.di);
varclickURL=$i(_fmm.t._0.cl);
if(clickURL.value==){
clickURL.value=
}
if(dispURL.value==){
dispURL.value=
}
}
doInit();
</script>