cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
ozs02
Level III

Force User to Provide Input Within Range

Hi, I'm fairly new to JMP and I'm not sure how to write a script that throws an error to prevent the user from inputting an invalid number. I want the user to input a number that is eventually saved into a variable but re-prompt for input if the number is outside the range 1-4.

 

This is what I have so far without any error checking:

 

Names Default To Here (1);
num = 0;

w = New Window( "Prompt",
	<<Modal,
	<<return result,
	V List Box(
		Text Box( "Enter Number Between 1 and 4" ),
			str1 = Number Edit Box(num)
	),
);

input = w["str1"];

show(input);

Any help or suggestions on accomplishing this would be appreciated!

 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions
jthi
Super User

Re: Force User to Provide Input Within Range

I think you can use << On Validate() for that.

 

Names Default To Here(1);

If(
	ex = New Window("Dialog() example",
		<<Modal,
		<<Return Result,
		<< On Validate(1 <= variable << get <= 4),
		V List Box(
			H List Box("Set this value", variable = Number Edit Box(42)),
			H List Box(Button Box("OK"), Button Box("Cancel"))
		)
	);
	ex["button"] == 1;
,
	ex["variable"],
	"CANCEL"
);

 

Edit:

Or did you mean greying out/disabling the button?

Names Default To Here(1);

If(
	ex = New Window("Dialog() example",
		<<Modal,
		<< return result,
		V List Box(
			H List Box("Set this value", variable = Number Edit Box(42,
				<< set function(
					if(1 <= variable << get <= 4,
						btn1 << enable(1),
						btn1 << enable(0)
					);
				)
			)),
			H List Box(btn1 = Button Box("OK", << enable(0)), Button Box("Cancel"))
		)
	);
	ex["button"] == 1;
,
	ex["variable"],
	"CANCEL"
);
-Jarmo

View solution in original post

pmroz
Super User

Re: Force User to Provide Input Within Range

If you reference files on a fileshare that all users can access it would work.

You could also include the programs to run in the main JSL code itself as separate functions.  Here's an example:

// First program
program1 = function({},
	show("Running program1");
// JSL code for first program

);

// Second program
program2 = function({},
	show("Running program2");
// JSL code for second program

);

// Third program
program3 = function({},
	show("Running program3");
// JSL code for third program

);

// Fourth program
program4 = function({},
	show("Running program4");
// JSL code for fourth program

);

nw = new window("Test Options",
	panel box("Click the button to run the desired program",
		lineup box(ncol(1),
			b1 = button box("First Program",
				program1();
			),
			b2 = button box("Second Program",
				program2();
			),
			b3 = button box("Third Program",
				program3();
			),
			b4 = button box("Fourth Program",
				program4();
			),
		),
	),
	bexit = button box("Exit",
		nw << close window();
	),
);

Take a look at the JSL documentation for more information on functions.

View solution in original post

20 REPLIES 20
jthi
Super User

Re: Force User to Provide Input Within Range

You could use set function in some way.

Below is an example where the value is set to 1 if user tries to input value below 1 and to 4 if user inputs above 4

Names Default To Here (1);
num = 1;

w = New Window( "Prompt",
	<<Modal,
	<<return result,
	V List Box(
		Text Box( "Enter Number Between 1 and 4" ),
			str1 = Number Edit Box(num, 
				<< Set Function(Function({this},
					If(this << get < 1, this << set(1),
						this << get > 4, this << set(4)
					);
					
				))
			)
	),
);

input = w["str1"];

show(input);

 

 

-Jarmo
ozs02
Level III

Re: Force User to Provide Input Within Range

Hi, thanks so much for the reply! I would like to make it so the user is forced to chose a number in range rather than have the code adjust to the user's mistake. For example, I'm thinking each number will tie to a piece of code that will be run.

 

Names Default To Here (1);
num = 0;
w = New Window( "Prompt", <<Modal, <<return result, V List Box( Text Box( "Enter Number Between 1 and 4" ), Text Box( "1- Option 1" ), Text Box( "2- Option 2" ), Text Box( "3- Option 3" ), Text Box( "4- Option 4" ), Text Box( "" ), str1 = Number Edit Box(num) ), ); input = w["str1"]; show(input);

Then afterwards, I will code an if statement where if input == 1, run this code, if input == 2, run a different piece of code and so forth. So for my situation, I don't want the number to auto-correct to 1 or 4 if out of range. Sorry if that was confusing!

jthi
Super User

Re: Force User to Provide Input Within Range

Wouldn't it be simpler to use Radio Box for implementation like that?

Names Default To Here(1);
New Window("Example", rb = Radio Box({"True", "False", "None of the above"}, Print("Selection Changed")));
-Jarmo
pmroz
Super User

Re: Force User to Provide Input Within Range

If you want to run different programs depending on what users select (1-4), I would use buttons instead.  Here's what it would look like:

pmroz_0-1627492690741.png

Here's the code.  Note that I'm using panel box to surround the buttons, and lineup box to align both sides of the button boxes.  The programs to be run are stored in c:\myfiles, and are called program1.jsl, program2.jsl etc.  Change the folder and JSL program names to suit your situation.

nw = new window("Test Options",
	panel box("Click the button to run the desired program",
		lineup box(ncol(1),
			b1 = button box("First Program",
				include("c:\myfiles\program1.jsl");
				nw << close window();
			),
			b2 = button box("Second Program",
				include("c:\myfiles\program2.jsl");
				nw << close window();
			),
			b3 = button box("Third Program",
				include("c:\myfiles\program3.jsl");
				nw << close window();
			),
			b4 = button box("Fourth Program",
				include("c:\myfiles\program4.jsl");
				nw << close window();
			),
		),
	),
	bexit = button box("Exit",
		nw << close window();
	),
);
ozs02
Level III

Re: Force User to Provide Input Within Range

Hi! That's a great approach to the problem. I am wondering though, would I be able to share all of the program scripts so anyone with the codes can run the same programs linked to the buttons? Or would it not be possible if the file directory paths would be unique to anyone who downloads the .jsl scripts?

pmroz
Super User

Re: Force User to Provide Input Within Range

If you reference files on a fileshare that all users can access it would work.

You could also include the programs to run in the main JSL code itself as separate functions.  Here's an example:

// First program
program1 = function({},
	show("Running program1");
// JSL code for first program

);

// Second program
program2 = function({},
	show("Running program2");
// JSL code for second program

);

// Third program
program3 = function({},
	show("Running program3");
// JSL code for third program

);

// Fourth program
program4 = function({},
	show("Running program4");
// JSL code for fourth program

);

nw = new window("Test Options",
	panel box("Click the button to run the desired program",
		lineup box(ncol(1),
			b1 = button box("First Program",
				program1();
			),
			b2 = button box("Second Program",
				program2();
			),
			b3 = button box("Third Program",
				program3();
			),
			b4 = button box("Fourth Program",
				program4();
			),
		),
	),
	bexit = button box("Exit",
		nw << close window();
	),
);

Take a look at the JSL documentation for more information on functions.

ozs02
Level III

Re: Force User to Provide Input Within Range

That's great! I think that would work for my purposes, thanks for sharing! Lastly, this is not a super important functionality, but do you know if there is a way to press the button which executes the program and then the window closes automatically without the user pressing 'exit' ?

pmroz
Super User

Re: Force User to Provide Input Within Range

Just add a close window after the function call.

			b1 = button box("First Program",
				program1();
				nw << close window();
			),
ozs02
Level III

Re: Force User to Provide Input Within Range

Re-edit: It actually seems that any code coming after the button execution within the new window is running BEFORE the programs linked to the button for some reason. Does anyone know why this is happening?