Data entry Mask - Enforce how it is entered

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Data entry Mask - Enforce how it is entered

Post by kpassaur » Fri Aug 16, 2013 3:57 pm

Is there an easy to use option to enforce how data is entered into a text box in a dialog? For instance there is an option for numbers only. I was trying to find one where I would specify a date format.

I was thinking of adding a calendar, but it would really be to slow. What I would be after is something like XX-XX-XXXX. Another example would be a phone number XXX-XXX-XXXX.

Really jus the way you specify to format a cell in Excel.

I know I could check each time the user leaves the text box for dashes, numbers etc. But I really don't want to go that route and later find out it was a simple mask/filter option that I missed in the help file or forum.

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Post by JRL » Fri Aug 16, 2013 9:06 pm

Field masks may be in Macro Scheduler's future but there are none today that I'm aware of.

Here's a sample date mask using two edit boxes. One that is accepting the typed information and one that is displaying the typed result. The one that we type into is sized to 0 x 0 pixels so it is accessible yet invisible. The one that displays the information is disabled so it is visible but inaccessible. There is also an empty image object that sits atop the visible dialog, If it is clicked the invisible dialog is focused and the backspace key is pressed.

The month and day fields are evaluated while typing is taking place for impossible dates. The year is evaluated for leap year if the month is February and the day is 29.

Of course the caveat is that this is not simple. But it was fun to develop. So here it is. If you can use it great, if not, nothing lost.

Code: Select all

Dialog>Dialog1
object Dialog1: TForm
  Position = poScreenCenter
  Caption = 'Date Mask Sample'
  ClientHeight = 119
  ClientWidth = 299
  object Label1: TLabel
    Left = 96
    Top = 16
    Width = 98
    Height = 13
    Caption = 'Date MM/DD/CCYY'
  end
  object Edit1: TEdit
    Left = 113
    Top = 35
    Width = 64
    Height = 21
    Enabled = False
    NumbersOnly = True
    TabOrder = 0
    Text = '__/__/____'
  end
  object Edit2: TEdit
    Left = 0
    Top = 0
    Width = 0
    Height = 0
    MaxLength = 8
    NumbersOnly = True
    TabOrder = 1
  end
  object MSImage1: tMSImage
    Left = 113
    Top = 35
    Width = 64
    Height = 21
  end
  object MSButton1: tMSButton
    Left = 113
    Top = 80
    Width = 64
    Height = 25
    Caption = 'Ok'
    TabOrder = 10
    Default = True
  end
end
EndDialog>Dialog1

AddDialogHandler>Dialog1,MSButton1,OnClick,Process
AddDialogHandler>Dialog1,Edit2,OnKeyUp,DateMask
AddDialogHandler>Dialog1,MSImage1,OnClick,SetFocusToEdit2

Show>dialog1,res1

SRT>DateMask
  If>DateMAsk_Key=37
    Press Right
    Press Backspace
  EndIf
  GetDialogProperty>Dialog1,edit2,text,datedata
  Length>datedata,strlen
  If>strlen<8
    Let>FinalDate={"FinalDate"}
  EndIf
  If>strlen=0
    Let>datedata=__/__/____
    Goto>EndDateMask
  EndIf
  MidStr>datedata,1,2,mm
  If>%mm%>12
    Let>mm=12
  EndIf
  MidStr>datedata,3,2,dd
  If>{(%mm%=02)and(%dd%>29)}
    Let>dd=29
  EndIf
  If>{((%mm%=04)or(%mm%=06)or(%mm%=09)or(%mm%=11))and(%dd%>30)}
    Let>dd=30
  EndIf
  If>{((%mm%=01)or(%mm%=03)or(%mm%=05)or(%mm%=07)or(%mm%=08)or(%mm%=10)or(%mm%=12))and(%dd%>31)}
    Let>dd=31
  EndIf
  MidStr>datedata,5,4,ccyy
  If>strlen=1
      Let>datedata=%mm%_/__/____
  EndIf
  If>strlen=2
      Let>datedata=%mm%/__/____
  EndIf
  If>strlen=3
      Let>datedata=%mm%/%dd%_/____
  EndIf
  If>strlen=4
      Let>datedata=%mm%/%dd%/____
  EndIf
  If>strlen=5
      Let>datedata=%mm%/%dd%/%ccyy%___
  EndIf
  If>strlen=6
      Let>datedata=%mm%/%dd%/%ccyy%__
  EndIf
  If>strlen=7
      Let>datedata=%mm%/%dd%/%ccyy%_
  EndIf
  If>{(%strlen%>=8)}
    If>FinalDate<>{"FinalDate"}
      Let>datedata=%FinalDate%
    Else
      Let>modres={%ccyy% mod 4}
      If>modres<>0
        If>{(%mm%=02)and(%dd%=29)}
          Let>dd=28
        EndIf
      EndIf
      Let>datedata=%mm%/%dd%/%ccyy%
      Let>FinalDate=%mm%/%dd%/%ccyy%
    EndIf
  EndIf
  Label>EndDateMask
  SetDialogProperty>Dialog1,edit1,text,datedata
END>DateMask

SRT>SetFocusToEdit2
  SetDialogObjectFocus>Dialog1,Edit2
  Press BackSpace
END>SetFocusToEdit2

SRT>Process
  GetDialogProperty>Dialog1,edit1,text,datedata
  Length>datedata,strlen
  If>Strlen=10
    MDL>datedata
  Else
    MDL>"%datedata%" is an incomplete or malformed date.  Please re-enter
  EndIf
END>Process

kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

date mask

Post by kpassaur » Sat Aug 17, 2013 8:02 am

Thanks, it is quite clever. It has given me some ideas as to how I may incorporate a mask into mine. Yours is a little to complex to be added to what I have but I may have to use it. Currently I have four boxes stacked on top of each other with one being visible. Which would be okay, but I have 6 of these and four variations of them in one script.

So I am thinking that instead of a mask I just may go with a date evaulation this way it could be flexible. For instance after it is entered it is passed to a subroutine that verifies that it is a real date and then one that matches the pattern. I'm thinking two EasyPatterns. Not as slick as yours but it would get the job done.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts