Attachment 'datecheck.js'

Download

   1 /*
   2  * datecheck.js
   3  *
   4  * $Author: $
   5  * $Date: $
   6  * $Revision: $
   7  *
   8  * ValidateForm()
   9  */
  10 
  11 var ValidateDate = {
  12   initialize: function() {
  13     // Declaring valid date character, minimum year and maximum year
  14     this._DATE_DELIMITER = "/";
  15     this._MIN_YEAR = 1900;
  16     this._MAX_YEAR = 2100;
  17   },
  18 
  19   _isInteger: function(s) {
  20     var i;
  21 
  22     for(i = 0; i < s.length; i++) {
  23       // Check that current character is number.
  24       var c = s.charAt(i);
  25 
  26       if(((c < "0") || (c > "9"))) {
  27         return false;
  28       }
  29     }
  30 
  31     // All characters are numbers.
  32     return true;
  33   },
  34 
  35   _stripCharsInBag: function(s, bag) {
  36     var i;
  37     var returnString = "";
  38 
  39     // Search through string's characters one by one.
  40     // If character is not in bag, append to returnString.
  41     for(i = 0; i < s.length; i++) {
  42       var c = s.charAt(i);
  43 
  44       if(bag.indexOf(c) == -1) {
  45         returnString += c;
  46       }
  47     }
  48 
  49     return returnString;
  50   },
  51 
  52   _daysInFebruary: function(year) {
  53     // February has 29 days in any year evenly divisible by four,
  54     // EXCEPT for centurial years which are not also divisible by 400.
  55     return (((year % 4 == 0) && ( (!(year % 100 == 0)) ||
  56               (year % 400 == 0))) ? 29 : 28 );
  57   },
  58 
  59   _daysArray: function(n) {
  60     for(var i = 1; i <= n; i++) {
  61       this[i] = 31;
  62 
  63       if(i == 4 || i == 6 || i == 9 || i == 11) {
  64         this[i] = 30;
  65       }
  66 
  67       if(i == 2) {
  68         this[i] = 29;
  69       }
  70     }
  71 
  72     return this;
  73   },
  74 
  75   validateDate: function(value) {
  76     var daysInMonth = this._daysArray(12);
  77     var pos1 = value.indexOf(this._DATE_DELIMITER);
  78     var pos2 = value.indexOf(this._DATE_DELIMITER, pos1 + 1);
  79     var strMonth = value.substring(0, pos1);
  80     var strDay = value.substring(pos1 + 1, pos2);
  81     var strYear = value.substring(pos2 + 1);
  82     var strYr = strYear;
  83 
  84     if(strDay.charAt(0) == "0" && strDay.length > 1) {
  85       strDay=strDay.substring(1);
  86     }
  87 
  88     if(strMonth.charAt(0) == "0" && strMonth.length > 1) {
  89       strMonth=strMonth.substring(1);
  90     }
  91 
  92     for(var i = 1; i <= 3; i++) {
  93       if(strYr.charAt(0) == "0" && strYr.length > 1) {
  94         strYr = strYr.substring(1);
  95       }
  96     }
  97 
  98     month = parseInt(strMonth);
  99     day = parseInt(strDay);
 100     year = parseInt(strYr);
 101 
 102     if(pos1 == -1 || pos2 == -1) {
 103       throw "The date format should be : mm/dd/yyyy";
 104     }
 105 
 106     if(strMonth.length < 1 || month < 1 || month > 12) {
 107       throw "Please enter a valid month";
 108     }
 109 
 110     if(strDay.length < 1 || day < 1 || day > 31 ||
 111        (month == 2 && day > this._daysInFebruary(year)) ||
 112         day > daysInMonth[month]) {
 113       throw "Please enter a valid day";
 114     }
 115 
 116     if(strYear.length != 4 || year == 0 || year < this._MIN_YEAR ||
 117        year > this._MAX_YEAR) {
 118       throw "Please enter a valid 4 digit year between " + this._MIN_YEAR +
 119             " and " + this._MAX_YEAR;
 120     }
 121 
 122     if(value.indexOf(this._DATE_DELIMITER, pos2 + 1) != -1 ||
 123        this._isInteger(this._stripCharsInBag(value,
 124                         this._DATE_DELIMITER)) == false) {
 125       throw "Please enter a valid date";
 126     }
 127   }
 128 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.