Subscribe Bookmark RSS Feed

How to check valid inputs in a Modal Dialog

orthogonal

Community Trekker

Joined:

Aug 30, 2013

I have written a jsl script which inputs three positive integers and creates a table.  It works fine if the user gives valid inputs.  My question is how can I verify the inputs to ensure that they are numeric and integers?  Additionally, I would like to have the checking done within the modal dialog so that it will check for invalid input and give the user an opportunity to correct it.  Lastly, my cancel button doesn't seem to abort the script.

Any help is appreciated.


//Create training, cross validation and test, column for a covariate input to DOE



New Window( "Create Cross Validation Covariate Table",


<<Modal,   


    Text Box( "Enter the number of runs for each set"),


    V List Box(


        Line up box(Ncol(2),


            Text Box("Training"), ntrain_in = number Edit Box(100),


            Text Box("Cross Validation"), ncv_in = number Edit Box(10),


            Text Box("Test"), ntest_in = number Edit Box(10),


        ),


    ),   


   


    Button Box("OK",


   


        //What do I need to do to put the input checking here and only accept the


        //input if valid?


   


        ntrain = ntrain_in <<get;


        ncv = ncv_in <<get;


        ntest = ntest_in <<get;


    ),


    Button Box("Cancel",


        Throw("Cancel Button Selected")


        //this doesn't seem to work.  Any ideas?


    )


);



Show(ntrain, ncv, ntest);



//Need help on checking if valid input.


//each input should be a positive integer in the range [0,large integer]


if( OR(ntrain<0,ntrain==""),


    show(ntrain);


    // how to check if numeric?


    ntrain=0;   


    throw("ntrain value not valid")


);



M1 = J(1,ntrain,0);


M2 = J(1,ncv,1);


M3 = J(1,ntest,2);



cv_dt = New Table("Cross Validation for Covariate input to DOE",


    New Column("validation",Numeric, continuous,


        Set Values(concat(concat(M1,M2,M3)))


    )


);



1 ACCEPTED SOLUTION

Accepted Solutions
ms

Super User

Joined:

Jun 23, 2011

Solution

I think it would be better to do the checking before pressing "OK" (which typically closes a modal window... then it's late for regrets).

However, NumberEditBox() seems not to support scripts. Below is a variant of your example using NumberColEditBox() instead. Every edit activates a script that disables the OK button if one or more non-valid numbers are entered.

nw = New Window( "Create Cross Validation Covariate Table",

  <<Modal,

  Table Box(

  String Col Box( " ", {"Training", "Cross Validation", "Test"} ),

  nb = Number Col Edit Box(

  "Positive Integers only",

  [100, 10, 10],

  m = nb << get as matrix;

  // disable OK-button if non-valid number is entered

  bb << enable( 1 - Max( Is Missing( m ) | m < 0 | Floor( m ) != m ) );

  )

  ),

  bb = Button Box( "OK", m = nb << get as matrix ),

  Button Box( "Cancel" )

);

If( Arg( nw[1] ) == -1, Stop()); // Stop execution if "Cancel"

M1 = J( 1, m[1], 0 );

M2 = J( 1, m[2], 1 );

M3 = J( 1, m[3], 2 );

cv_dt = New Table( "Cross Validation for Covariate input to DOE",

  New Column( "validation",

  Numeric,

  continuous,

  Set Values( Concat( Concat( M1, M2, M3 ) ) )

  )

);

2 REPLIES
ms

Super User

Joined:

Jun 23, 2011

Solution

I think it would be better to do the checking before pressing "OK" (which typically closes a modal window... then it's late for regrets).

However, NumberEditBox() seems not to support scripts. Below is a variant of your example using NumberColEditBox() instead. Every edit activates a script that disables the OK button if one or more non-valid numbers are entered.

nw = New Window( "Create Cross Validation Covariate Table",

  <<Modal,

  Table Box(

  String Col Box( " ", {"Training", "Cross Validation", "Test"} ),

  nb = Number Col Edit Box(

  "Positive Integers only",

  [100, 10, 10],

  m = nb << get as matrix;

  // disable OK-button if non-valid number is entered

  bb << enable( 1 - Max( Is Missing( m ) | m < 0 | Floor( m ) != m ) );

  )

  ),

  bb = Button Box( "OK", m = nb << get as matrix ),

  Button Box( "Cancel" )

);

If( Arg( nw[1] ) == -1, Stop()); // Stop execution if "Cancel"

M1 = J( 1, m[1], 0 );

M2 = J( 1, m[2], 1 );

M3 = J( 1, m[3], 2 );

cv_dt = New Table( "Cross Validation for Covariate input to DOE",

  New Column( "validation",

  Numeric,

  continuous,

  Set Values( Concat( Concat( M1, M2, M3 ) ) )

  )

);

orthogonal

Community Trekker

Joined:

Aug 30, 2013

Thanks for the great example of how to code for input parameter checking.