常用js函数

ExpandedBlockStart.gif ContractedBlock.gif /**/ /*------------------------------------------------------------
InBlock.gif dateDlg(inputid,initDate,startYear,endYear)  弹出时间窗口
InBlock.gif compareDate(end,s)                           与当前时间比较
InBlock.gif compareTwoDate(startDate,endDate,s)          比较两个时间
InBlock.gif isNumber(text,name)                          判断全是数字(true)                          
InBlock.gif isChar(text,addtemp,name,include)            判断符合要求字符
InBlock.gif isEmail(text)                                判断是电子邮件
InBlock.gif isPid(text)                                  判断是身份证号
InBlock.gif isNull(text,name)                            判断为空则提示(true)
InBlock.gif getLength(text)                              获取长度
InBlock.gif lengthEquals(text,name,num)                  确定长度(true)
InBlock.gif lengthless(text,name,num)             长度不少于(true)
InBlock.gif lengthmore(text,name,num)             长度不大于(true)
InBlock.gif        checkPassword(text,text1)                    检验两次输入密码是否一致(true)
InBlock.gif  ------------------------------------------------------------ 
InBlock.gif//checkData()放在jsp页面
InBlock.gif<script language="JavaScript" src="check.js"></script>
InBlock.gif<script language="JavaScript">
InBlock.giffunction checkData()
InBlock.gif{
InBlock.gif if(isNull(form1.name,'帐号')){}
InBlock.gif else if(lengthEquals(form1.name,"帐号",8)){}
InBlock.gif else if (getLength(form1.name)>13){}
InBlock.gif else{
InBlock.gif  form1.submit();
InBlock.gif }  
InBlock.gif}
InBlock.gif</script>
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 弹出时间选择小控件
InBlock.gif inputid--文本框name
InBlock.gif initDate--初始日期,为空时是当前时间
InBlock.gif startYear--日期范围的开始日期
InBlock.gif endYear--日期范围的结束日期
InBlock.gif 使用例子onClick="dateDlg(end,'1999-11-12','1980','2010')"
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  dateDlg(inputid,initDate,startYear,endYear)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif 
var pattern = /^(19|20)([0-9])dot.gif{2}$/;
InBlock.gif flag
=pattern.test(startYear);
InBlock.gif 
if(!flag)startYear=1900;
InBlock.gif flag
=pattern.test(endYear);
InBlock.gif 
if(!flag)endYear=2050;
InBlock.gif 
if(inputid.value==null||inputid.value=='')
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
if(initDate==null||initDate=='')
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif     currentDate 
= new Date();  
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gifContractedSubBlock.gif   
elsedot.gif{
InBlock.gif    currentDate 
= initDate;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gifContractedSubBlock.gif 
elsedot.gif
InBlock.gif   currentDate 
= inputid.value;
ExpandedSubBlockEnd.gif  }

InBlock.gif 
var arguments = new Array(startYear,endYear,0,0,0)
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif 
var pattern = /^(19|20)([0-9])dot.gif{2}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/;
InBlock.gif flag
=pattern.test(currentDate);
InBlock.gif 
if(flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  iYear
=currentDate.substring(0,4);
InBlock.gif  iMonth
=currentDate.substring(5,7);
InBlock.gif  iDay
=currentDate.substring(8,10);
InBlock.gif  arguments 
= new Array(startYear,endYear,iYear,iMonth,iDay)
ExpandedSubBlockEnd.gif }

InBlock.gif showx 
= event.screenX - event.offsetX + 18;
InBlock.gif showy 
= event.screenY - event.offsetY - 210;
InBlock.gif
InBlock.gif 
var features =
InBlock.gif  'dialogWidth:'  
+ 192 + 'px;' +
InBlock.gif  'dialogHeight:' 
+ 210 + 'px;' +
InBlock.gif  'dialogLeft:'   
+ showx     + 'px;' +
InBlock.gif  'dialogTop:'    
+ showy     + 'px;' +
InBlock.gif  'directories:no; localtion:no; menubar:no; status
=no; toolbar=no;scrollbars:yes;Resizeable=no';
InBlock.gif retval 
= window.showModalDialog("../js/calendar.htm", arguments , features );
InBlock.gif 
var calctrl = eval(inputid)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
if( retval != null )dot.gif{
InBlock.gif  calctrl.value 
= retval;
ExpandedSubBlockStart.gifContractedSubBlock.gif }
elsedot.gif{
InBlock.gif  
//alert("canceled");
ExpandedSubBlockEnd.gif
 }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif Trim()去左右空格
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
None.gifString.prototype.Trim 
=   function ()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return this.replace(/(^\s*)|(\s*$)/g, "");
ExpandedBlockEnd.gif}

None.gifString.prototype.LTrim 
=   function ()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return this.replace(/(^\s*)/g, "");
ExpandedBlockEnd.gif}

None.gifString.prototype.Rtrim 
=   function ()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
return this.replace(/(\s*$)/g, "");
ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 判断密码是否有单引号
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  isNotYinhao(s)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {   
InBlock.gif    
var yin;
InBlock.gif 
var temp="'";
InBlock.gif 
for(yin=0; yin < s.length; yin++ ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif
InBlock.gif  
var ch = s.charAt(yin);
InBlock.gif  
if(temp.indexOf(ch)>=0)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   
return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
return false;
ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 定义变量
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
var  i,j;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 和当前日期比较,如果当前日期大于输入日期则提示
InBlock.gif end----输入日期
InBlock.gif s----提示信息
InBlock.gif 使用例子onClick="compareDate(end,'选择日期不能在今天之前!')" 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

ExpandedBlockStart.gifContractedBlock.gif
function  compareDate(end,s) dot.gif {
InBlock.gif
var a=new Date();
InBlock.gif
var b=end.value;
InBlock.gif
if(((Number(a.getYear())-Number(b.substring(0,4)))*356+
InBlock.gif       (Number(a.getMonth())
-Number(b.substring(5,7))+1)*31+
InBlock.gif    (Number(a.getDate())
-Number(b.substring(8,10))))>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  alert(s);
InBlock.gif  end.focus();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 两个时间进行比较,当开始日期大于结束日期则提示
InBlock.gif startDate----开始日期
InBlock.gif endDate------结束日期
InBlock.gif 使用例子onClick="compareTwoDate(startDate,endDate,'开始日期不能大于结束日期!')" 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  compareTwoDate(startDate,endDate,s)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif
var a=startDate.value;
InBlock.gif
var b=endDate.value;
InBlock.gif
if(((Number(a.substring(0,4))-Number(b.substring(0,4)))*356+
InBlock.gif       (Number(a.substring(
5,7))-Number(b.substring(5,7)))*31+
InBlock.gif    (Number(a.substring(
8,10))-Number(b.substring(8,10))))>0)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  alert(s);
InBlock.gif  startDate.focus();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 判断是否为数字类型,如不是数字类型则提示
InBlock.gif text-------输入文本
InBlock.gif name-------提示的名字
InBlock.gif 使用例子onBlur="compareTwoDate(this,'电话号码')" 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  isNumber(text,name)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
var temp="0123456789";
InBlock.gif   
for(j=0; j<text.value.length; j++ ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{    
InBlock.gif     
var ch = text.value.Trim().charAt(j);
InBlock.gif  
if(temp.indexOf(ch)==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   alert(name
+"应为数字类型!");
InBlock.gif   text.focus();
InBlock.gif   
return true;
ExpandedSubBlockEnd.gif  }
  
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 判断两次密码输入是否一致
InBlock.gif text-------新密码
InBlock.gif name-------再次输入新密码
InBlock.gif 使用例子checkPassword(form1.newpass,form1.newpass1) 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  checkPassword(text,text1)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
var newpass=text.value.Trim();
InBlock.gif  
var newpass1=text1.value.Trim();
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(newpass!=newpass1)dot.gif{
InBlock.gif    alert(
"两次输入新密码不一致!");
InBlock.gif    text.focus();
InBlock.gif    
return true;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 判断是否包含非法字符,如含非法字符则提示
InBlock.gif text-------输入文本
InBlock.gif addtemp----除英文和数字外还可包含的字符
InBlock.gif name-------提示的名字
InBlock.gif include----提示中不允许包含的字符
InBlock.gif 使用例子onBlur="compareTwoDate(this,'@_','邮件','%*$')" 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  isChar(text,addtemp,name,include)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
var temp="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"+addtemp;
InBlock.gif   
for(j=0; j<text.value.length; j++ ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{    
InBlock.gif     
var ch = text.value.Trim().charAt(j);
InBlock.gif  
if(temp.indexOf(ch)==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   alert(name
+"中不允许包含'"+include+"'等字符!");
InBlock.gif   text.focus();
InBlock.gif   
break
ExpandedSubBlockEnd.gif  }
  
ExpandedSubBlockEnd.gif   }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 判断输入的是否为电子邮件,如含非法字符则提示
InBlock.gif text-------输入的电子邮件
InBlock.gif 使用例子onBlur="isEmail(this)" 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  isEmail(text)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {   
InBlock.gif 
var email=text.value.Trim();
InBlock.gif 
var m=email.indexOf("@");
InBlock.gif 
var n=email.indexOf(".");
InBlock.gif 
if(email!="")
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  
if(m<1||m>email.length-3)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   alert(
"请输入正确的电子邮件格式!");
InBlock.gif   text.focus();
InBlock.gif   
return true;
ExpandedSubBlockEnd.gif  }

InBlock.gif  
else if(n<m+2||n>email.length-2)
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif   alert(
"请输入正确的电子邮件格式!");
InBlock.gif   text.focus();
InBlock.gif   
return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 判断输入文本是否为身份证号码,如为不正确则提示
InBlock.gif text-------输入的身份证号码
InBlock.gif 使用例子onBlur="isPid(this)" 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  isPid(text)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
var pid=text.value.Trim();
InBlock.gif 
var temp="0123456789";
InBlock.gif 
var temp1="0123456789xX";
ExpandedSubBlockStart.gifContractedSubBlock.gif 
if(pid!="")dot.gif{
InBlock.gif 
if(pid.length==15)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif     
for(j=0; j<15; j++ ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{    
InBlock.gif   
var ch = pid.charAt(j);
InBlock.gif   
if(temp.indexOf(ch)==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    alert(
"请输入正确的身份证号码!");
InBlock.gif    text.focus();
InBlock.gif    
break
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
       
ExpandedSubBlockEnd.gif }

InBlock.gif 
else if(pid.length==18)
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif
InBlock.gif     
for(j=0; j<pid.length-1; j++ ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif     
dot.gif{    
InBlock.gif   
var ch = pid.charAt(j);
InBlock.gif   
if(temp.indexOf(ch)==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    alert(
"请输入正确的身份证号码!");
InBlock.gif    text.focus();
InBlock.gif    
break
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }
       
InBlock.gif  
var ch1 = pid.charAt(pid.length-1);
InBlock.gif  
if(temp1.indexOf(ch1)==-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif   
dot.gif{
InBlock.gif    alert(
"请输入正确的身份证号码!");
InBlock.gif    text.focus();
ExpandedSubBlockEnd.gif   }
    
ExpandedSubBlockEnd.gif }

ExpandedSubBlockStart.gifContractedSubBlock.gif 
elsedot.gif{
InBlock.gif  alert(
"身份证号码的应为15位或18位!");
InBlock.gif  text.focus();
ExpandedSubBlockEnd.gif }
}

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 判断输入文本是否为空,如为空则提示
InBlock.gif text-------输入文本
InBlock.gif 使用例子onBlur="isNull(this,'姓名')" 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  isNull(text,name)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
if(text.value.Trim()==null||text.value.Trim()=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif 
dot.gif{
InBlock.gif  alert(name
+"不能为空!");
InBlock.gif  text.focus();
InBlock.gif  
return true;
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.gifContractedBlock.gif
/**/ /*------------------------------------------------------------
InBlock.gif 获取文本框长度,中文作为两个字符处理
InBlock.gif text-------输入文本
InBlock.gif 使用例子getLength(form1.name) 
ExpandedBlockEnd.gif  ------------------------------------------------------------
*/

None.gif
function  getLength(text)
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif 
var temp="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
InBlock.gif temp
=temp+"`~!@#$%^&*()_+|-=\[]{};':,./<>?\"";
InBlock.gif temp=temp+
"·~!◎#¥%……※×()——+§-=÷【】『』;‘:“,。、《》?";
InBlock.gif var len = text.value.Trim().length;
InBlock.gif for(j=0;j<text.value.Trim().length;j++)
InBlock.gif {
InBlock.gif  var ch= text.value.Trim().charAt(j);
InBlock.gif  if(temp.indexOf(ch)==-1){
InBlock.gif  len++;
InBlock.gif  }
InBlock.gif }
InBlock.gif return len;
InBlock.gif}
InBlock.gif
InBlock.gif/*------------------------------------------------------------
InBlock.gif lengthEquals(text,name,num)    相等长度
InBlock.gif lengthless(text,name,num)      长度不少于
InBlock.gif lengthmore(text,name,num)      长度不大于
InBlock.gif 获取文本框长度,中文作为两个字符处理
InBlock.gif text-------输入文本
InBlock.gif 使用例子getLength(form1.name,'姓名',8) 
InBlock.gif  ------------------------------------------------------------*/
InBlock.giffunction lengthEquals(text,name,num)
InBlock.gif{
InBlock.gif if(getLength(text)!=num)
InBlock.gif {
InBlock.gif  alert(
"请输入"+num+""+name+"!")
InBlock.gif  text.focus();
InBlock.gif  return true;
InBlock.gif }
InBlock.gif}
InBlock.gif
InBlock.giffunction lengthless(text,name,num)
InBlock.gif{
InBlock.gif if(getLength(text)<num)
InBlock.gif {
InBlock.gif  alert(name+
"不能少于"+num+"!")
InBlock.gif  text.focus();
InBlock.gif  return true;
InBlock.gif }
InBlock.gif}
InBlock.giffunction lengthmore(text,name,num)
InBlock.gif{
InBlock.gif if(getLength(text)>num)
InBlock.gif {
InBlock.gif  alert(name+
"不能大于"+num+"!")
InBlock.gif  text.focus();
InBlock.gif  return true;
InBlock.gif }
InBlock.gif}
InBlock.gif
InBlock.gif/*------------------------------------------------------------
InBlock.gif 判断bodyText输入文本是否为空,如为空则提示
InBlock.gif bodyText-------输入文本
InBlock.gif 使用例子onBlur=
"isNullBody('姓名')" 
InBlock.gif  ------------------------------------------------------------*/
InBlock.giffunction isNullBody(name)
InBlock.gif{
InBlock.gif  var bodyText = frames[
"Dvbbs_Composition"].document.body.innerText;
InBlock.gif  if(bodyText.length==0) {
InBlock.gif      frames[
"Dvbbs_Composition"].focus();
InBlock.gif      alert(name+
"不能为空!");
InBlock.gif      return true;
InBlock.gif  }
InBlock.gif}
InBlock.gif
InBlock.gif
InBlock.gif/*------------------------------------------------------------
InBlock.gif 获取文本框长度,中文作为两个字符处理
InBlock.gif bodyText-------输入文本
InBlock.gif 使用例子isLengthBody(form1.name) 
InBlock.gif  ------------------------------------------------------------*/
InBlock.giffunction isLengthBody(text,name)
InBlock.gif{
InBlock.gif    if (text.value.length > 600) {
InBlock.gif        frames[
"Dvbbs_Composition"].focus();
InBlock.gif        alert(name+
"不能超过600!");
InBlock.gif return true;
InBlock.gif    }
InBlock.gif}
InBlock.gif
InBlock.gif
InBlock.gif/*------------------------------------------------------------
InBlock.gif 检查是否含有”'“号
InBlock.gif        有,则返回true
InBlock.gif        无, 则返回false
InBlock.gif  ------------------------------------------------------------*/
InBlock.giffunction isTheChar(text,name)
InBlock.gif{
InBlock.gif    var re= /'/g;
InBlock.gif    var arr = text.match(re);
InBlock.gif    if (arr == null)
InBlock.gif        return false;
InBlock.gif    else
InBlock.gif        return true;
InBlock.gif}
InBlock.gif
InBlock.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值