ACL Scripts Part #5 – Validating User Input

If you have been trying to build scripts following my previous posts, then you are ready to make your scripts a little more robust; particularly if your scripts will be used by other people.  There are always issues when you prompt the user for input such as: did they actual provide any input; and is the input the right type and correct format.  Since the proper running of the script will often depend on the user input you should check for, and validate, the user input.

However, before we discuss how you can do that, I would like to reiterate the fact that when you prompt the user for input, you should provide direction.  For example, instead of simply saying “Please enter cut-off date”, you could provide additional information and direction: “Please provide cut-off date in the format ‘YYYY/MM/DD’.  The cut-off date will be used to determine the AR aging.”    An ounce of prevention is worth a pound of cure – if you can assist the user in making the appropriate input, you will have a more robust script.

Note: a number of the examples below use the ACCEPT command but the approach would apply to a DIALOG Box with an Edit Box for user input as well (see final examples).

Testing for, and Validating, User Input

The issue with the ACCEPT Command and the Dialog Box to prompt for user input is that once these commands have been run, ACL automatically moves to the next line of code.

ACCEPT “Enter maximum sole source amount (No dollar sign, commas or decimals)” to v_cont_max

SET FILTER Cont_Value  >  VALUE(v_cont_max,0) – using VALUE() since v_cont_max is a character variable

EXTRACT RECORD to Over_Sole_Source_Amt

 

Given the code above, ACL will prompt the user for the sole source contracting limit and then try to use this limit to create a filter.  If the user does not enter and amount or enters non-numeric values, the SET FILTER command will produce an error.  One option to ensure that the user has entered a value is to hard code a default value:

ACCEPT “Enter maximum sole source amount (No dollar sign, commas or decimals)” to v_cont_max

IF VALUE(v_cont_max,0) <=0 v_cont_max = “2500”

SET FILTER Cont_Value  >  VALUE(v_cont_max,0)

 

However, this may not always be a viable solution.  You might not know an appropriate entry and the user would not be aware that a default value was being used.  The second option is to put the ACCEPT command or Dialog Box in a sub-script and to execute the sub-script until the user provides a valid entry.  The Sub-script will use the IF COMMAND to test for, and validate, the user entry.   For example:

Main Script

v_valid_amt = F

DO Get_Max_Cont_Value WHILE v_valid_amt = F

SET FILTER Cont_Value  >  VALUE(v_cont_max,0)

Remainder of main script

 

Sub-Script – Get_Max_Cont_Value

ACCEPT “Enter maximum sole source amount (No dollar sign, commas or decimals)” to v_cont_max

IF VALUE(v_cont_max,0) <=0  PAUSE “Please enter a valid maximum contract amount”

IF VALUE(v_cont_max) > 0 v_valid_amt = T

 

(The main script sets v_valid_amt to False; calls the sub-script (DO Get_Max_Cont_Value) and keeps executing the sub-script until v_valid_amt is not False (WHILE v_valid_amt = F).

If the user does not enter a value or enters a non-numerical value, the sub-script will display a message (“Please enter a valid maximum contract amount”) and prompts the user again (ACCEPT “Enter maximum sole source amount (No dollar sign, commas or decimals)” to v_cont_max).

If the user supplies a valid numerical value that is greater than 0, v_valid_amt is set to True and the sub-script returns control to the main script which applies the filter – SET FILTER Cont_Value  >  VALUE(v_cont_max,0).

Unfortunately, the sub-script can test for a valid numerical entry, but it has no way of knowing if the user entry is correct (i.e. if sole source maximum is 25,000 and the user entered 10,000 the script would accept this as a valid entry and continue).  Another issue is the fact that this is an infinite loop.  ACL will continue to tell the user to enter a value and prompt for the value until a valid numerical value is received.

Sometimes, I give additional information to the user if they have failed to provide a valid input twice.  First I initialize a counter in the main script.  This is the number of times the sub-script has been called.  Then I use the counter to determine which message to display for the user.

 

Main Script

v_valid_amt = F

v_tries = 1

DO Get_Max_Cont_Value WHILE v_valid_amt = F

SET FILTER Cont_Value  >  VALUE(v_cont_max,0)

 

Sub-Script Sub-Script – Get_Max_Cont_Value

ACCEPT “Enter maximum sole source amount (No dollar sign, commas or decimals)” to v_cont_max

IF VALUE(v_cont_max,0) <=0  AND v_tries <= 2 PAUSE “Please enter a valid maximum contract amount”

IF VALUE(v_cont_max,0) <=0  AND v_tries >2 PAUSE “Please enter a valid maximum contract amount OR Click on Cancel to stop the script

IF VALUE(v_cont_max) > 0 v_valid_amt = T

V_tries = v_tries + 1

 

The same validation approach can be used to test user input of Dates or Character values.

 

Sub-script: Date user input

ACCEPT  “Please provide cut-off date in the format YYYY/MM/DD.  The cut-off date will be used to determine the AR aging.”   To v_cutoff_date

IF  CTOD(v_cutoff_date, “YYYY/MM/DD”) <= `19000101`  PAUSE “Please enter a valid cut-off date in the format YYYY/MM/DD”

IF  CTOD(v_cutoff_date, “YYYY/MM/DD”) > `19000101`  v_valid_date = T

In this case you may want to restrict user input to a small range e.g. a fiscal year:

IF  NOT BETWEEN(CTOD(v_cutoff_date, “YYYY/MM/DD”) , `20180101`, `20181231`)  PAUSE “Please enter a valid cut-off date (FY 2018) in the format YYYY/MM/DD”

IF  BETWEEN(CTOD(v_cutoff_date, “YYYY/MM/DD”) , `20180101`, `20181231`)  v_valid_date = T

 

Sub-script: Character user input

ACCEPT  “Please enter a plant number between 01 and 32 (without quotes).  The plant number will be used to restrict analysis to the selected plant.”   To v_plant

IF  NOT BETWEEN(v_plant, “01”, “30”)  PAUSE “Please enter a valid plant number”

IF  BETWEEN(v_plant, “01”, “30”)   v_valid_plant = T

 

DIALOG Box – Project Item Selection

Sub-script prompting user to select a Table

DIALOG (DIALOG TITLE “User Dialog” WIDTH 436 HEIGHT 251 ) (BUTTONSET TITLE “&OK;&Cancel” AT 132 192 DEFAULT 1 HORZ ) (TEXT TITLE “Select table” AT 48 28 ) (ITEM TITLE “f” TO “v_infile” AT 132 48 WIDTH 189 HEIGHT 124 )

IF FTYPE(“%v_infile%”) <> “y” PAUSE “Please select a table to be used by the analysis”

IF FTYPE(“%v_infile%”) = “y” v_valid_table = T

 

Sub-script prompting user to select a field

DIALOG (DIALOG TITLE “User Dialog” WIDTH 436 HEIGHT 251 ) (BUTTONSET TITLE “&OK;&Cancel” AT 132 192 DEFAULT 1 HORZ ) (TEXT TITLE “Select field” AT 48 28 ) (ITEM TITLE “C” TO “v_field” AT 132 48 WIDTH 189 HEIGHT 124 )

IF NOT ISDEFINED(“%v_field%”) PAUSE “Please select a feild to be used by the analysis”

IF ISDEFINED(“%v_field%”) v_valid_field = T

 

DIALOG BOX – Drop-Down List

DIALOG (DIALOG TITLE “User Dialog” WIDTH 436 HEIGHT 251 ) (BUTTONSET TITLE “&OK;&Cancel” AT 132 192 DEFAULT 1 HORZ ) (TEXT TITLE “Select Location” AT 36 28 ) (DROPDOWN TITLE “Ottawa;New York;Vancouver” TO “v_loc” AT 144 72 )

IF ISBLANK(“%v_loc%”) PAUSE “Please select a location to be used by the analysis”

IF NOT ISBLANK(“%v_loc%”) v_valid_loc = T

 The user is not typing an entry, thus the input can only be blank (did not select an item) or valid (selected an item).

 

DIALOG BOX – Check Box

Since check boxes can take only take on values of True or False, you cannot test to determine if the user accidentally left a check box unchecked.  One option would be to set the default to checked, and the user would have to uncheck the box if they did not want to select that item.

You can also test to ensure that at least one check box has been checked.

DIALOG (DIALOG TITLE “User Dialog” WIDTH 433 HEIGHT 279 ) (BUTTONSET TITLE “&OK;&Cancel” AT 120 216 DEFAULT 1 HORZ ) (TEXT TITLE “Select Locations (at least one)” AT 36 28 ) (CHECKBOX TITLE “Ottawa” TO “v_loc1” AT 84 72 CHECKED) (CHECKBOX TITLE “New York” TO “v_loc2” AT 84 108 ) (CHECKBOX TITLE “Vancouver” TO “v_loc3” AT 84 144 )

IF NOT MATCH(T, v_loc1, v_loc2, v_loc3) PAUSE “Please select at least one location to be used by the analysis”

IF MATCH(T, v_loc1, v_loc2, v_loc3) v_valid_loc = T

 

DIALOG BOX – Radio Button

DIALOG (DIALOG TITLE “User Dialog” WIDTH 436 HEIGHT 251 ) (BUTTONSET TITLE “&OK;&Cancel” AT 132 192 DEFAULT 1 HORZ ) (TEXT TITLE “Select Location” AT 36 28 ) (RADIOBUTTON TITLE “Ottawa;New York;Vancouver;Exit” TO “v_loc” AT 120 72 DEFAULT 1)

Since the Radio button allows you to set a default – the user cannot leave this prompt blank.

 

These are simple examples of IF COMMANDs that can be used to test user input.  You may need to provide a more robust test and validation of user input.  The approach will be the same, but you may be able to supply tighter criteria to the test.  For example, you could remove the $ and commas from a numeric entry before testing the validity.  Good luck.

Next post ???? TBD.

 

Leave a Reply

Your email address will not be published.