純javascript判斷查詢?nèi)掌谑欠駷橛行掌?/h1>
更新時間:2015年08月24日 11:02:12 作者:人界的游鬼
很多網(wǎng)站都涉及到輸入日期選項,如果客戶日期輸入錯誤,可能導入查詢不到甚至查詢到錯誤的信息,為了更好的滿足用戶需求,需要對日期進行校驗,下面給大家介紹使用純javascript如何判斷查詢?nèi)掌谑欠駷橛行掌?,需要的朋友可以參考?/div>
以下內(nèi)容主要通過js代碼給大家介紹,代碼比較簡單,包含注釋,有好的建議歡迎提出。
如下圖,當查詢條件含有日期時,如“2012-3-4”,查詢前校驗輸入的日期字符串是否為有效的日期

var snapshot_createTime_begin=$(selector+" input[name='createTime_begin']").val().trim();
var snapshot_createTime_end=$(selector +" input[name='createTime_end']").val().trim();
try{
//判斷開始時間是否為有效的日期
if(snapshot_createTime_begin!=""&&new Date(snapshot_createTime_begin).getDate()!=snapshot_createTime_begin.match(/-\d{0,2}$/g)[0].replace(/-/g,"")){
throw new Error();
}
//判斷結束時間是否為有效的日期
if(snapshot_createTime_end!=""&&new Date(snapshot_createTime_end).getDate()!=snapshot_createTime_end.match(/-\d{0,2}$/g)[0].replace(/-/g,"")){
throw new Error();
}
if(Date.parse(snapshot_createTime_begin)>Date.parse(snapshot_createTime_end)){
//alert("開始日期不應當超過結束日期!");
alert("開始日期不應當超過結束日期!");
return ;
}
$.extend(pageObj,{
createTimeBegin:snapshot_createTime_begin,
createTimeEnd:snapshot_createTime_end,
});
initPagination();
}catch(e){
//alert("請輸入有效日期!")
alert("請輸入有效日期!");
}
js判斷年月日是否一個有效日期
function isdate(intYear,intMonth,intDay){
if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay)) return false;
if(intMonth>12||intMonth<1) return false;
if ( intDay<1||intDay>31)return false;
if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30)) return false;
if(intMonth==2){
if(intDay>29) return false;
if((((intYear%100==0)&&(intYear%400!=0))||(intYear%4!=0))&&(intDay>28))return false;
}
return true;
}
以上代碼就是對日期的有效性校驗,希望對大家有所幫助。
相關文章
-
淺談javascript中字符串String與數(shù)組Array
這篇文章主要介紹了淺談javascript中字符串String與數(shù)組Array,需要的朋友可以參考下 2014-12-12
-
undefined==null引發(fā)的兩者區(qū)別與聯(lián)系
undefined==null引發(fā)的兩者區(qū)別與聯(lián)系 2007-12-12
-
javascript中mouseover、mouseout使用詳解
這篇文章主要介紹了javascript中mouseover、mouseout使用詳解的相關資料,需要的朋友可以參考下 2015-07-07
-
Bootstrap3.0建站教程(一)之bootstrap表單元素排版
本文給大家介紹Bootstrap3.0建站教程(一)之bootstrap表單元素排版,本文給大家列舉了文字和輸入框前后排列和上下排列的實例代碼,有需要的朋友參考下吧 2016-06-06
最新評論
以下內(nèi)容主要通過js代碼給大家介紹,代碼比較簡單,包含注釋,有好的建議歡迎提出。
如下圖,當查詢條件含有日期時,如“2012-3-4”,查詢前校驗輸入的日期字符串是否為有效的日期
var snapshot_createTime_begin=$(selector+" input[name='createTime_begin']").val().trim(); var snapshot_createTime_end=$(selector +" input[name='createTime_end']").val().trim(); try{ //判斷開始時間是否為有效的日期 if(snapshot_createTime_begin!=""&&new Date(snapshot_createTime_begin).getDate()!=snapshot_createTime_begin.match(/-\d{0,2}$/g)[0].replace(/-/g,"")){ throw new Error(); } //判斷結束時間是否為有效的日期 if(snapshot_createTime_end!=""&&new Date(snapshot_createTime_end).getDate()!=snapshot_createTime_end.match(/-\d{0,2}$/g)[0].replace(/-/g,"")){ throw new Error(); } if(Date.parse(snapshot_createTime_begin)>Date.parse(snapshot_createTime_end)){ //alert("開始日期不應當超過結束日期!"); alert("開始日期不應當超過結束日期!"); return ; } $.extend(pageObj,{ createTimeBegin:snapshot_createTime_begin, createTimeEnd:snapshot_createTime_end, }); initPagination(); }catch(e){ //alert("請輸入有效日期!") alert("請輸入有效日期!"); }
js判斷年月日是否一個有效日期
function isdate(intYear,intMonth,intDay){ if(isNaN(intYear)||isNaN(intMonth)||isNaN(intDay)) return false; if(intMonth>12||intMonth<1) return false; if ( intDay<1||intDay>31)return false; if((intMonth==4||intMonth==6||intMonth==9||intMonth==11)&&(intDay>30)) return false; if(intMonth==2){ if(intDay>29) return false; if((((intYear%100==0)&&(intYear%400!=0))||(intYear%4!=0))&&(intDay>28))return false; } return true; }
以上代碼就是對日期的有效性校驗,希望對大家有所幫助。
相關文章
淺談javascript中字符串String與數(shù)組Array
這篇文章主要介紹了淺談javascript中字符串String與數(shù)組Array,需要的朋友可以參考下2014-12-12undefined==null引發(fā)的兩者區(qū)別與聯(lián)系
undefined==null引發(fā)的兩者區(qū)別與聯(lián)系2007-12-12javascript中mouseover、mouseout使用詳解
這篇文章主要介紹了javascript中mouseover、mouseout使用詳解的相關資料,需要的朋友可以參考下2015-07-07Bootstrap3.0建站教程(一)之bootstrap表單元素排版
本文給大家介紹Bootstrap3.0建站教程(一)之bootstrap表單元素排版,本文給大家列舉了文字和輸入框前后排列和上下排列的實例代碼,有需要的朋友參考下吧2016-06-06