zoukankan      html  css  js  c++  java
  • js 判断日期是否正确

      写了一个js function  在这里跟大伙分享一下

    我们项目 是这样的 年份和月份是selectBox类型 Day是TextBox类型 所以只需要判断 Day 输入是否合理就行

    //create by Elvin 2013-12-10
        function checkDay(dayCp,dayId,monthId,yearId)
          {
          var day=getValue(dayId);
          var month=getValue(monthId);
          var year=getValue(yearId);
          var strP =/^d+$/;
          var oNumValue = getValue(dayId);
          if (!oNumValue) {
              return;
          }
          if(!strP.test(oNumValue))
          {
              fsAlert('expectedformats|' + dayCp + '|99');
              document.getElementById(dayId).focus();          
          }
          var dayInt=parseInt(day);
          if(month=="09")
          {
          month=month.substr(1,1);
          }
          var monthInt=parseInt(month);
          var yearInt=parseInt(year);
          if ((monthInt == 4) || (monthInt == 6) || (monthInt == 9) || (monthInt == 11)) {
              if (dayInt > 30) {
                  fsAlert('expectedformats|' + dayCp + '|1-30');
                  document.getElementById(dayId).focus();
              }
          } else if (monthInt == 2) {
              if ((yearInt % 4 == 0 && yearInt % 100 != 0) || (yearInt % 400 == 0)) {
                  if (dayInt > 29) {
                      fsAlert('expectedformats|' + dayCp + '|1-29');
                      document.getElementById(dayId).focus();
                  }
              } else {
                  if (dayInt > 28) {
                      fsAlert('expectedformats|' + dayCp + '|1-28');
                      document.getElementById(dayId).focus();
                  }
              }
          } else {
          if (dayInt > 31) {
              fsAlert('expectedformats|' + dayCp + '|1-31');
              document.getElementById(dayId).focus();
          }
          }
            
          
          }

    if(month=="09")  這裡 爲什麽要month=month.substr(1,1);呢 其實我也特別納悶  但是但我選擇09 的時候 他parseInt之後 依然是09 所以沒辦法只有截取了。

    還請各位大神賜教。

  • 相关阅读:
    最短路径算法
    二叉树遍历的应用
    二叉搜索树
    二叉树的遍历
    Linux 用户和用户组管理-用户信息文件
    Linux脚本安装包
    Linux 源码包安装过程
    RPM包管理-yum在线管理
    Linux RPM命令查询
    Linux RPM管理命令
  • 原文地址:https://www.cnblogs.com/longshaokun/p/3469368.html
Copyright © 2011-2022 走看看