Javascript之杂

Javascript Trim Stand-Alone Functions

If you prefer not to modify the string prototype, then you can use the stand-alone functions below.


function trim(stringToTrim) {
return stringToTrim.replace(/^/s+|/s+$/g,"");
}
function ltrim(stringToTrim) {
return stringToTrim.replace(/^/s+/,"");
}
function rtrim(stringToTrim) {
return stringToTrim.replace(//s+$/,"");
}

// example of using trim, ltrim, and rtrim
var myString = " hello my name is ";
alert("*"+trim(myString)+"*");
alert("*"+ltrim(myString)+"*");
alert("*"+rtrim(myString)+"*");

 

JS (Javascript)取当前日期、时间的代码

<SCRIPT LANGUAGE="JavaScript">
<!--
var myDate = new Date();
    myDate.getYear();      //获取当前年份(2位)
    myDate.getFullYear();  //获取完整的年份(4位,1970-????)
    myDate.getMonth();      //获取当前月份(0-11,0代表1月)
    myDate.getDate();      //获取当前日(1-31)
    myDate.getDay();        //获取当前星期X(0-6,0代表星期天)
    myDate.getTime();      //获取当前时间(从1970.1.1开始的毫秒数)
    myDate.getHours();      //获取当前小时数(0-23)
    myDate.getMinutes();    //获取当前分钟数(0-59)
    myDate.getSeconds();    //获取当前秒数(0-59)
    myDate.getMilliseconds();  //获取当前毫秒数(0-999)
    myDate.toLocaleDateString();    //获取当前日期
    var mytime=myDate.toLocaleTimeString();    //获取当前时间
    myDate.toLocaleString( );      //获取日期与时间
if (mytime<"23:30:00")
{
alert(mytime);
}
//-->
</SCRIPT>

 

//Javscript正则表达式验证表达

//01/31/2008 22:03

function DateFormatCheck(obj)
  {
   var rr=/^(?:(?:0[1-9])|(?:1[0-2]))//(?:(?:[0-2][1-9])|(?:[1-3][0-1]))//(?:19|20)[0-9][0-9] (?:(?:[0-2][0-3])|(?:[0-1][0-9])):[0-5][0-9]$/
   
   obj.value = trim(obj.value);
   if(!rr.test(obj.value))
   {
      alert('Please Check The Format,Example:01/27/2008 22:12');
      obj.focus();
      return false;
   }
   if (!isDate(obj.value))
   {
    alert('The Date Is Wrong !');
    obj.focus();
    return false;
   }
   return true;
  }

  function   isDate(sDate)  
  {    
    sDate   =   sDate.toString();
   
    var monthOfDate = sDate.substring(0,2);
    var dayOfDate = sDate.substring(3,5);
    var yearOfDate = sDate.substring(6,10);
   
    sDate = (sDate.substring(0,10));
   
    var tempDate = new Date(Date.parse(sDate));
    var tempYear = tempDate.getYear();
    var tempMonth = tempDate.getMonth() + 1;
    var tempDate = tempDate.getDate();
   
    if(yearOfDate == tempYear && monthOfDate == tempMonth && dayOfDate == tempDate)
    {
   return   true;
    }else{
     return   false;  
    }  
  }

控件调用:

 <td>
       <input type="text" name="jEffectiveDate2" value="" onBlur="DateFormatCheck(this)" style="width:100%"
       ><br/></td><td><img src="../../images/calendar1.gif" onClick="return showCalendar('jEffectiveDate2', '%m/%d/%Y %H:%M', '24');" width="13" height="13" />
      </td>

 

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值