3 Replies Latest reply on May 14, 2009 8:34 AM by Barna Biro

    How to use a Date validator for time only? (H:M A)

    EvolvedDSM Level 2

      I have a form that contains a field for time.  It's crucial that proper time format is used here.  I need to include the hour, the minute, and AM or PM.  (10:30 AM for instance).  I've tried googling and am probably not using the correct keywords because I'm not getting anything for time validation, only date validation.


      Can anyone provide some code for time-only validation?


      I do have access to <validation:TimeValidator> as a ColdFusion class (com.adobe.ColdFusion.validation).  This comes up in my mxml code, but I have no clue as to how it's used.


      Whatever is the simpliest way to validate time is all I need to know.  thanks!

        • 1. Re: How to use a Date validator for time only? (H:M A)
          Barna Biro Level 3

          Hi there,


          May I ask: why don't you slipt 10:30 PM into [10, :, 30, , PM]  and then read the minutes and validate it with a small function you can write in no time? Simply check if the passed minutes are more than or equal to 0 and less than or equal to 60, else, throw an error message.


          With best regards,

          Barna Biro

          Blog: http://blog.wisebisoft.com

          • 2. Re: How to use a Date validator for time only? (H:M A)
            EvolvedDSM Level 2

            Well I was looking for something as simple as this


                <mx:DateValidator id="thisdate"
                    source="{dateInput}" property="text"

            But for time instead.


            I don't have much experience with delimiters and splitting text.  If you have some time to show how this is done, I would appreciate it.

            • 3. Re: How to use a Date validator for time only? (H:M A)
              Barna Biro Level 3

              Hi again,


              Here's a fast example ( I wrote this in an ActionScript project that's why I used "throw new Error" so I can display a message saying if the passed minutes value is valid or not valid; you'll obviously use something else for your application, maybe an Alert or something ).


                  import flash.display.Sprite;
                  import flash.display.StageAlign;
                  import flash.display.StageScaleMode;
                  import flash.events.MouseEvent;
                  import flash.text.TextField;
                  import flash.text.TextFieldType;
                  public class ValidateTimeExample extends Sprite
                      * @private
                      * Reference to the input field.
                      private var tf:TextField;
                      * Constructor.
                      public function ValidateTimeExample()
                          // Some movie settings.
                          stage.scaleMode = StageScaleMode.NO_SCALE;
                          stage.showDefaultContextMenu = false;
                          stage.align = StageAlign.TOP_LEFT;
                          // Create the input field where users can type a time after the
                          // following model: HOUR:MIN PERIOD ( example: 10:24 PM ) 
                          // The default value is 10:59 AM
                          tf = new TextField();
                          tf.type = TextFieldType.INPUT;
                          tf.border = true;
                          tf.width = 200;
                          tf.height = 22;
                          tf.multiline = false;
                          tf.text = "10:59 AM";
                          // Create a button button that will validate the user input on click.
                          var btn:Sprite = new Sprite();
                          btn.graphics.drawRect(0, 0, tf.width, 22);
                          btn.y = tf.height + 5;
                          btn.buttonMode = true;
                          btn.addEventListener(MouseEvent.CLICK, onValidate);
                      * @private
                      * Triggered when the user clicks the red button.
                      private function onValidate(event:MouseEvent):void
                          // Get and store the minutes from the user input.
                          var minutes:Number = getMinute(tf.text);
                          // Check if the minutes entered by the user are valid or not.
                          // Throw a message to show the validation result.
                          if (minutes < 0 || minutes > 60) throw new Error("Invalid minutes: " + minutes + "min");
                          else throw new Error("Valid minutes: " + minutes + "min");
                      * @private
                      * Used to slipt the value entered by the user into pieces and extract only
                      * the minutes from the text.
                      * @return Returns the minutes from the passed text.
                      private function getMinute(time:String):Number
                          // Get the minutes from the passed string.
                          var timeValue:Number = Number(time.split(" ")[0].toString().split(":")[1]);
                          return timeValue;


              In essence, what you need is the getMinute method. Once you get the time as a Number from the passed text, you can simply compare it and decide what to do or not depending on the validation result. Keep in mind that this example only works if the time entered by the user is of this format: 10:20 AM if you try a different format, it will not work ( for that, you need to adapt the getMinute method so it will split the correct part out of the data ).


              With best regards,

              Barna Biro

              Blog: http://blog.wisebisoft.com