1、js去表单中字符中是否有空格

 

function trim(inputString) {
    
              if (typeof inputString != "string") { return inputString; }
              var retValue = inputString;
              var ch = retValue.substring(0, 1);
              while (ch == " ") { 
           //检查字符串开始部分的空格
                  retValue = retValue.substring(1, retValue.length);
                  ch = retValue.substring(0, 1);
              }
              ch = retValue.substring(retValue.length-1, retValue.length);
              while (ch == " ") {
                 //检查字符串结束部分的空格
                 retValue = retValue.substring(0, retValue.length-1);
                 ch = retValue.substring(retValue.length-1, retValue.length);
              }
              while (retValue.indexOf("  ") != -1) { 
          //将文字中间多个相连的空格变为一个空格
                 retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); 
              }
              return retValue;
           } 

 

2、js 弹出提示层,并锁定页面

 

 
  <body>
   <a href="PAGE/MyJsp.jsp">This a struts page.</a>  <br>
   <input type="button" value="点击这里" onclick="sAlert('test弹窗效果');" />
   
  </body>
</html:html>
<script type="text/javascript" language="javascript">
//more javascript from http://www.smallrain.net
        function sAlert(str){
        var msgw,msgh,bordercolor;
        msgw=400;//提示窗口的宽度
        msgh=100;//提示窗口的高度
        titleheight=25 //提示窗口标题高度
        bordercolor="#c51100";//提示窗口的边框颜色
        titlecolor="#c51100";//提示窗口的标题颜色
        
        var sWidth,sHeight;
        sWidth=screen.width;
        sHeight=screen.height;

        var bgObj=document.createElement("div");
        bgObj.setAttribute('id','bgDiv');
        bgObj.style.position="absolute";
        bgObj.style.top="0";
        bgObj.style.background="#cccccc";
        bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
        bgObj.style.opacity="0.6";
        bgObj.style.left="0";
        bgObj.style.width=sWidth + "px";
        bgObj.style.height=sHeight + "px";
        bgObj.style.zIndex = "10000";
        document.body.appendChild(bgObj);
        
        var msgObj=document.createElement("div")
        msgObj.setAttribute("id","msgDiv");
        msgObj.setAttribute("align","center");
        msgObj.style.background="white";
        msgObj.style.border="1px solid " + bordercolor;
        msgObj.style.position = "absolute";
        msgObj.style.left = "50%";
        msgObj.style.top = "50%";
        msgObj.style.font="12px/1.6em Verdana, Geneva, Arial, Helvetica, sans-serif";
        msgObj.style.marginLeft = "-225px" ;
        msgObj.style.marginTop = -75+document.documentElement.scrollTop+"px";
        msgObj.style.width = msgw + "px";
        msgObj.style.height =msgh + "px";
        msgObj.style.textAlign = "center";
        msgObj.style.lineHeight ="25px";
        msgObj.style.zIndex = "10001";

       var title=document.createElement("h4");
       title.setAttribute("id","msgTitle");
       title.setAttribute("align","right");
       title.style.margin="0";
       title.style.padding="3px";
       title.style.background=bordercolor;
       title.style.filter="progid:DXImageTransform.Microsoft.Alpha(startX=20, startY=20, finishX=100, finishY=100,style=1,opacity=75,finishOpacity=100);";
       title.style.opacity="0.75";
       title.style.border="1px solid " + bordercolor;
       title.style.height="18px";
       title.style.font="12px Verdana, Geneva, Arial, Helvetica, sans-serif";
       title.style.color="white";
       title.style.cursor="pointer";
       title.innerHTML="关闭";
       title.onclick=function(){
    document.body.removeChild(bgObj);
    document.getElementById("msgDiv").removeChild(title);
document.body.removeChild(msgObj);
            }
       document.body.appendChild(msgObj);
       document.getElementById("msgDiv").appendChild(title);
       var txt=document.createElement("p");
       txt.style.margin="1em 0"
       txt.setAttribute("id","msgTxt");
       txt.innerHTML=str;
       document.getElementById("msgDiv").appendChild(txt);
        }
    </script>

 

3、js中的日期处理

 

function cc(dd,dadd)
{
//可以加上错误处理
var a = new Date(dd)
a = a.valueOf()
a = a +dadd * 24 * 60 * 60 * 1000
a = new Date(a)
return (a.getFullYear() + "-" + (a.getMonth() + 1) + "-" + a.getDate() )
}

//日期格式化
function mapOnClick()
		{
                 var string = (document.form1.endtime.value).split("-");
                 var string2 = (document.form1.endtime2.value).split("-");
                // alert(new Date());
                 var myDate = new Date(string[0],string[1]-1,string[2]);
                 var myDate2 = new Date(string2[0],string2[1]-1,string2[2]);
                 //取当前日期
                 var newYear=new Date().getYear();
                 var newMonth=new Date().getMonth();
                 if(newMonth==12){
                   newMonth=1;
                 }else{
                   newMonth=newMonth+1;
                 }

                 var newDay=new Date().getDate();

               if(myDate2>new Date()){
                 //alert(newYear+"-"+newMonth+"-"+newDay);
                 document.form1.endtime2.value=newYear+"-"+newMonth+"-"+newDay;
               }
               if(myDate>new Date()){
                 //alert(newYear+"-"+newMonth+"-"+newDay);
                 document.form1.endtime.value=newYear+"-"+newMonth+"-"+newDay;
               }
          

                 if(myDate2<myDate){
                   alert("截止日期不能大于起始日期,请重新选择!");
                   return flase;
                 }
                 
//alert(document.form1.endtime.value);
//var myDate = new Date(document.form1.endtime.value);
//alert(myDate);     
//日期相等比较
                if(Date.parse(myDate2)==Date.parse(myDate)){
                myDate=cc(myDate,1);
                //alert(myDate);
                // myDate.setDate(myDate.getDate()+1)
                document.form1.endtime2.value=myDate;
               }

			form1.map.value = "1";
                        form1.table1.value = "2";
			if( form1.endtime.value == "" || form1.endtime2.value == "" )
			{
				alert("请填写完整后,再执行!");
				return;
			}
			form1.submit();
		}
////////////////////////////////////////字符串类型与时间类型间的转换
 try{
               SimpleDateFormat   dateFormat   =   new   SimpleDateFormat("yyyy-MM-dd");
               Date date=dateFormat.parse(begin);
               long   dateMS   =   date.getTime();
               dateMS   =   dateMS   +   60   *   60   *   24   *   1000   *   1;
               date.setTime(dateMS);
               String   dateString   =   dateFormat.format(date);
               end=dateString;
              //System.out.println("date:::"+dateString);
           }catch(Exception e){
               e.printStackTrace();
           }

 

4、判断所有表单项非空

 

  function checkdate() {
     //alert("sadfsa");
   var   input = document.all.tags("input");   
   for(i=0;i<input.length;i++)   
  {   
    if   (trimString(input[i].value)=="")   
    {   
      alert("请填完整空白项信息!");   
      return   false;   
    }
  } 

 

5、实现页面指定时间后自动跳转

 

<body>
  <!-- url后面的为目标地址-->
<meta http-equiv="Refresh" content="5;url=目标位置">
  <table>
  <tr>
          <td width="181" height="30" class="FontBlack">  该页面</td>
          <td width="22" align="center" class="FontBlack"><div id="time">5</div></td>
          <td width="194" align="left" class="FontBlack">秒后自动跳转</td>
   </tr>
</table>
</body>

<script language="JavaScript">
	var times=6;
	clock();
	function clock()
	{
		window.setTimeout('clock()',1000);
		times=times-1;
		time.innerHTML =times;
	}
</script>
  

 

 

评论
xrb2008 2008-01-16
//将文字中间多个相连的空格变为一个空格 、
如果用户输入的内容中间有空格,怎么办。主要是为了照顾这个。
zhangyu313 2008-01-16
trim方法不用这么复杂吧
function trim(strParm) {
	return strParm.replace(/(^\s*)|(\s*$)/g,'')
}


这样也可以

String.prototype.trim(strParm) {
	return this.replace(/(^\s*)|(\s*$)/g,'')
}
发表评论

您还没有登录,请登录后发表评论