Saturday, February 4, 2012

AS3: Valid Date Validator + ASDoc

I used to search over the web for a AS3 date validator back then and after spending some time searching and trying, I am going to share the class that I have written in the past with you. XD
(Note: By the way I have added a bit of extra stuff in the AS3 file. Take a look and it might turn out to be pretty handy in code writing in the future. :D)

The following would be the source file of my Main Application in Flex.
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="absolute"
     creationComplete="creationCompleteHandler(event)">
 
 <mx:Script>
  <![CDATA[
   import com.date.DateChecker;
   
   import mx.events.FlexEvent;
   
   //String to store the minimum year
   private var tempTxtMin:Number;
   
   //This function will be executed onve the button have
   //been clicked.
   protected function button1ClickEvent(event:MouseEvent):void
   {
    if(txtMin.text == "")
    {
     tempTxtMin = -1;
    }else{
     tempTxtMin = Number(txtMin.text);
    }
    //The following will check the given fields and
    //check if they were valid dates.
    lblResult.text = digestErrorIntoString(
     DateChecker.shortStrDateChecker(txtDay.text,
     txtMonth.text,
     txtYear.text,
     tempTxtMin));
    lblResult1.text = digestErrorIntoString(
     DateChecker.strDateChecker(txtDate.text,
     "/",
     tempTxtMin));
    lblResult2.text = digestErrorIntoString(
     DateChecker.strDateChecker(txtDate1.text,
      "-",
      tempTxtMin));
   }
   
   //Base on the the result that you are getting in
   //DateChecker.strDateChecker or DateChecker.shortStrDateChecker
   //output a string for the respective results
   private function digestErrorIntoString(error:int):String
   {
    var resultStr:String = "";
    switch(error)
    {
     case 0:
      resultStr = "This is a valid Date.";
      break;
     case 1:
      resultStr = "Please enter a numeric number " +
       "for the day of the month.";
      break;
     case 2:
      resultStr = "Please enter a numeric number " +
      "for the month of the year.";
      break;
     case 3:
      resultStr = "Please enter a numeric number " +
      "for the year that you are query.";
      break;
     case 4:
      resultStr = "Please enter a date between 1 - 31.";
      break;
     case 5:
      resultStr = "Please enter a month between 1 - 12.";
      break;
     case 6:
      resultStr = "Please enter a year that is greater " +
       "than " + String(tempTxtMin) + ".";
      break;
     case 7:
      resultStr = "This date doesn't exist.";
      break;
     case 8:
      resultStr = "Please enter the date according to the" +
       " format that was shown in the above.";
      break;
    }
    return resultStr;
   }
   
   //Upon Creation Complete of this class, populate some content.
   //And do a check on the fields.
   protected function creationCompleteHandler(event:FlexEvent):void
   {
    // TODO Auto-generated method stub
    txtMin.text = "1960";
    txtDay.text = "9";
    txtMonth.text = "8";
    txtYear.text = "1965";
    txtDate.text = "9/8/1959";
    txtDate1.text = "30-2-2012";
    button1ClickEvent(null);
   }
   
  ]]>
 </mx:Script>
 
 <mx:VBox width="100%" height="100%" verticalGap="10"
    horizontalAlign="center"
    verticalAlign="middle">
  <mx:HBox width="100%" horizontalAlign="center"
     horizontalGap="0">
   <mx:Label text="Minimum Year Allow:"/>
   <mx:TextInput id="txtMin" restrict="0-9" maxChars="4"/>
  </mx:HBox>
  <mx:HBox width="100%" horizontalAlign="center"
     horizontalGap="0">
   <mx:Label text="Enter a date:"/>
   <mx:TextInput id="txtDay" restrict="0-9" maxChars="2"/>
   <mx:Label text="/"/>
   <mx:TextInput id="txtMonth" restrict="0-9" maxChars="2"/>
   <mx:Label text="/"/>
   <mx:TextInput id="txtYear" restrict="0-9" maxChars="4"/>
   <mx:Label text="(Ex: DD/MM/YYYY)"/>
  </mx:HBox>
  <mx:HBox width="100%" horizontalAlign="center"
     horizontalGap="0">
   <mx:Label text="Result of the above:"/>
   <mx:Label id="lblResult"/>
  </mx:HBox>
  <mx:HBox width="100%" horizontalAlign="center"
     horizontalGap="0">
   <mx:Label text="Enter a date:"/>
   <mx:TextInput id="txtDate" restrict="0-9/"/>
   <mx:Label text="(Ex: DD/MM/YYYY)"/>
  </mx:HBox>
  <mx:HBox width="100%" horizontalAlign="center"
     horizontalGap="0">
   <mx:Label text="Result of the above:"/>
   <mx:Label id="lblResult1"/>
  </mx:HBox>
  <mx:HBox width="100%" horizontalAlign="center"
     horizontalGap="0">
   <mx:Label text="Enter a date:"/>
   <mx:TextInput id="txtDate1" restrict="0-9 \-"/>
   <mx:Label text="(Ex: DD-MM-YYYY)"/>
  </mx:HBox>
  <mx:HBox width="100%" horizontalAlign="center"
     horizontalGap="0">
   <mx:Label text="Result of the above:"/>
   <mx:Label id="lblResult2"/>
  </mx:HBox>
  <!-- Upon clicking on this button, 
  run the event 'button1ClickEvent'-->
  <mx:Button label="Check Now" click="button1ClickEvent(event)"/>
 </mx:VBox>
</mx:Application>

and here comes the AS3 Date checker class

package com.date
{
 public class DateChecker
 {
  public function DateChecker()
  {
  }
  
  /**
   * Used to check if the given string is a valid date
   * @param str - the string than you are checking 
   * for a valid date
   * @param divider - a string that will be used as a 
   * divider for str
   * @param minYear - the minimum year than the user 
   * can enter
   * @return one of the below integer (0-8)
   *   
0 - valid date *
1 - Invalid day (day must be a numerical value) *
2 - Invalid Month (month must be a numerical value) *
3 - Invalid Year (year must be a numerical value) *
4 - Invalid day (day must be in a range of 1 - 31) *
5 - Invalid Month (month must be in a range of 1-12) *
6 - Invalid Year (year must be > than the * minYear specified) *
7 - Date doesn't exist *
8 - the paramter str is not in the correct format */ public static function strDateChecker(str:String, divider:String = "/", minYear:Number = -1):int { var _strArray:Array = String(str).split(divider); //If the array has more than 3 items (e.g. 11/11/20/11) //it is not a valid date format, return error code 8 if(_strArray.length != 3) { return 8; }else{ return shortStrDateChecker(_strArray[0], _strArray[1], _strArray[2], minYear); } } /** * Used to check if the given strings turn out to be * a valid date * @param day - the day of the date that you are checking * in string format * @param month - the month of the date that you are checking * in string format * @param year - the year of the date that you are checking * in string format * @param minYear - the minimum year than the user * can enter * @return one of the below integer (0-8) *
0 - valid date *
1 - Invalid day (day must be a numerical value) *
2 - Invalid Month (month must be a numerical value) *
3 - Invalid Year (year must be a numerical value) *
4 - Invalid day (day must be in a range of 1 - 31) *
5 - Invalid Month (month must be in a range of 1-12) *
6 - Invalid Year (year must be > than the * minYear specified) *
7 - Date doesn't exist *
8 - the paramter day, month and * year is not given in the correct format */ public static function shortStrDateChecker(day:String, month:String, year:String, minYear:Number = -1):int { var _day:Number = Number(day); var _month:Number = Number(month); var _year:Number = Number(year); var _date:Date = new Date(); //if all 3 given parameters are either strings //or empty, return error code 8 if((isNaN(_day) || day == "") && (isNaN(_month) || month == "") && (isNaN(_year) || year == "")) { return 8; } //if given date is a String, return error code 1 if(isNaN(_day)) { return 1; } //if given month is a String, return error code 2 if(isNaN(_month)) { return 2; } //if given year is a String, return error code 3 if(isNaN(_year)) { return 3; } _month = Number(_month - 1); //if given day is a > 31 and < 0, return error code 4 if(_day > 31 || _day <= 0) { return 4; } //if given month is a > 12 and < 0, return error code 5 if(_month > 11 || _month < 0) { return 5; } //if given year is bigger than minYear, return error code 6 if(minYear != -1) { if(_year < minYear) { return 6; } } _date.fullYear = _year; _date.month = _month; _date.date = _day; //if the temp date and the given date doesn tally //, return error code 7 if(_date.date != _day || _date.month != _month || _date.fullYear != _year) { return 7; } return 0; } } }
* Click here to view the demo of this example.
^ Click here for the source files of this demo.

2 comments:

  1. hi, Useful post for web developers .I have bookmarked your page for the further reference
    Thanks for this valueable post.
    Professional Web Design

    ReplyDelete
  2. shorter version: http://flashcodetips.blogspot.nl/2013/09/check-validity-of-given-date.html

    ReplyDelete