常用js函数

简介:
+关注继续查看

ExpandedBlockStart.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.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.gif
{
ExpandedSubBlockStart.gif 
var pattern = /^(19|20)([0-9]){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.gif  
{
InBlock.gif   
if(initDate==null||initDate=='')
ExpandedSubBlockStart.gif   
{
InBlock.gif     currentDate 
= new Date();  
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockStart.gif   
else{
InBlock.gif    currentDate 
= initDate;
ExpandedSubBlockEnd.gif   }

ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gif 
else
InBlock.gif   currentDate 
= inputid.value;
ExpandedSubBlockEnd.gif  }

InBlock.gif 
var arguments = new Array(startYear,endYear,0,0,0)
InBlock.gif
ExpandedSubBlockStart.gif 
var pattern = /^(19|20)([0-9]){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.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.gif 
if( retval != null ){
InBlock.gif  calctrl.value 
= retval;
ExpandedSubBlockStart.gif }
else{
InBlock.gif  
//alert("canceled");
ExpandedSubBlockEnd.gif
 }

ExpandedBlockEnd.gif}

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

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

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

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

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

None.gif
function isNotYinhao(s)
ExpandedBlockStart.gif
{   
InBlock.gif    
var yin;
InBlock.gif 
var temp="'";
InBlock.gif 
for(yin=0; yin < s.length; yin++ ) 
ExpandedSubBlockStart.gif 

InBlock.gif  
var ch = s.charAt(yin);
InBlock.gif  
if(temp.indexOf(ch)>=0)
ExpandedSubBlockStart.gif  
{
InBlock.gif   
return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

InBlock.gif 
return false;
ExpandedBlockEnd.gif}

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

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

ExpandedBlockStart.gif
function compareDate(end,s){
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.gif 
{
InBlock.gif  alert(s);
InBlock.gif  end.focus();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

None.gif
None.gif
ExpandedBlockStart.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.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.gif 
{
InBlock.gif  alert(s);
InBlock.gif  startDate.focus();
ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

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

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

ExpandedBlockEnd.gif}

None.gif
ExpandedBlockStart.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.gif
{
InBlock.gif  
var newpass=text.value.Trim();
InBlock.gif  
var newpass1=text1.value.Trim();
ExpandedSubBlockStart.gif  
if(newpass!=newpass1){
InBlock.gif    alert(
"两次输入新密码不一致!");
InBlock.gif    text.focus();
InBlock.gif    
return true;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

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

ExpandedBlockEnd.gif}

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

None.gif
function isEmail(text)
ExpandedBlockStart.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.gif 
{
InBlock.gif  
if(m<1||m>email.length-3)
ExpandedSubBlockStart.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.gif  
{
InBlock.gif   alert(
"请输入正确的电子邮件格式!");
InBlock.gif   text.focus();
InBlock.gif   
return true;
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockEnd.gif }

ExpandedBlockEnd.gif}

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

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

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

ExpandedBlockEnd.gif}

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

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

ExpandedBlockEnd.gif}

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

None.gif
function getLength(text)
ExpandedBlockStart.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



本文转自高海东博客园博客,原文链接:http://www.cnblogs.com/ghd258/archive/2005/11/18/279686.html,如需转载请自行联系原作者
相关文章
|
3天前
|
JavaScript 前端开发
javaScript—02 基础入门 (函数的应用)
javaScript—02 基础入门 (函数的应用)
|
3天前
|
存储 移动开发 JavaScript
JavaScript函数和BOM
JavaScript函数和BOM
24 0
|
3天前
|
JavaScript 前端开发 索引
原生JavaScript之函数特殊对象arguments
原生JavaScript之函数特殊对象arguments
18 0
|
3天前
|
JavaScript
JS-函数、对象与原型
JS-函数、对象与原型
15 0
|
13天前
|
JavaScript 前端开发
JavaScript decodeURI() 函数
JavaScript decodeURI() 函数
9 0
|
13天前
|
存储 JavaScript 前端开发
【JS交互埋坑】事件函数自动将数字字符串String转为数值Number
【JS交互埋坑】事件函数自动将数字字符串String转为数值Number
20 0
|
17天前
|
JavaScript 前端开发 Java
手写JavaScript防抖节流和休眠函数
防抖原理是在一定时间内,只有最后一次操作,再过延迟的时间后执行,一般用于防止用户多次重复点击按钮,只会触发最后一次
17 0
|
17天前
|
JavaScript 前端开发
javascript函数的call、apply和bind的原理及作用详解
javascript函数的 call、apply和bind 本质是用来实现继承的,专业点说法就是改变函数体内部 this 的指向,当一个对象没有某个功能时,就可以用这3个来从有相关功能的对象里借用过来
17 0
|
18天前
|
JavaScript
JS和JQUERY的入口函数,和他们的不同
JS和JQUERY的入口函数,和他们的不同
14 0
|
18天前
|
JavaScript 前端开发
推荐文章
更多