Home>

Many websites involve entering date options,If the customer date is entered incorrectly,May not import query information or even query wrong information,In order to better meet user needs,Need to verify the date,Here's how to use pure javascript to determine whether the query date is a valid date.

As shown in the figure below, when the query condition contains a date,For example, "2012-3-4", check whether the entered date string is a valid date before querying

var snapshot_createtime_begin=$(selector + "input [name =" createtime_begin "]"). val (). trim ();
      var snapshot_createtime_end=$(selector + "input [name =" createtime_end "]"). val (). trim ();
      try {
        //Judge whether the start time is a valid date
        if (snapshot_createtime_begin!=""&&new date (snapshot_createtime_begin) .getdate ()!=snapshot_createtime_begin.match (/-\ d {0,2} $/g) [0] .replace (/-/g, "")) {
          throw new error ();
        }
        //Determine whether the end time is a valid date
        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 ("The start date should not exceed the end date!");
          alert ("The start date should not exceed the end date!");
          return;
        }
        $.extend (pageobj, {
          createtimebegin:snapshot_createtime_begin,          createtimeend:snapshot_createtime_end,        });
        initpagination ();
      } catch (e) {
        //alert ("Please enter a valid date!")
        alert ("Please enter a valid date!");
      }

Js judging whether the year, month and day is a valid date

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;
}
  • Previous C # yield keyword
  • Next Method to completely delete the SQL Server registry