cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
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!

 

 

20 REPLIES 20
jthi
Super User

Re: Force User to Provide Input Within Range

If you have additional code after the New Window, you could change the new window to modal window with << modal (might require some other small changes). This example will also close the modal window if any of the program buttons is pressed by using <<Click():

nw = new window("Test Options", << modal,
	panel box("Click the button to run the desired program",
		lineup box(ncol(1),
			b1 = button box("First Program",
				program1();
				okBtn << Click();
			),
			b2 = button box("Second Program",
				program2();
				okBtn << Click();
			),
			b3 = button box("Third Program",
				program3();
				okBtn << Click();
			),
			b4 = button box("Fourth Program",
				program4();
				okBtn << Click();
			),
		),
	),
	h list box(okbtn = Button Box("OK"), cancelBtn=ButtonBox("Cancel"))
);

Show("shouldnt execute before window is closed");
-Jarmo
ozs02
Level III

Re: Force User to Provide Input Within Range

That works perfectly! Thanks again!

txnelson
Super User

Re: Force User to Provide Input Within Range

Here is a modification of your code that handles the requirement to only allow values 1-4

Names Default To Here( 1 );
num = 0;
input = 0;
w = New Window( "Prompt",
	<<Modal,
	<<return result,
	V List Box(
		Text Box( "Enter Number Between 1 and 4" ),
		str1 = Number Edit Box(
			num,
			10,
			<<SetFunction(
				Function( {this}, /* put my value into my sibling's display */
					If( str1 << get > 4 | str1 << get < 1,
						str1 << set( 0 )
					)
				)
			)
		),
		H List Box( Button Box( "Cancel" ), Button Box( "OK" ) )
	),

);

If( w["button"] == 1,
	input = w["str1"]
);

Show( input );
Jim
ozs02
Level III

Re: Force User to Provide Input Within Range

Would there be a way to prevent the user from pressing the OK button and exiting the window unless the value was 1-4? I think as it is, the user can exit the window and the input will be set to 0.

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
ozs02
Level III

Re: Force User to Provide Input Within Range

That's perfect! Thank you so much! :^) 

 

For learning purposes, what does the end of the code mean? I know usually its If(condition 1, result 1, condition 2, result 2, ..... resultElse); but I'm not familiar with this: 

	ex["button"] == 1;
,
	ex["variable"],
	"CANCEL

 

jthi
Super User

Re: Force User to Provide Input Within Range

That should be same, but it does look a bit weird. Using the modal example from Scripting Index:

 

This part is basically condition1 of the if:

 

	ex = New Window("Dialog() example",
		<<Modal,
		<<Return Result,
		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;

And this is the result1

 

 

ex["variable"]

and returnElse:

 

 

"CANCEL"

And because those results and basically last lines which will be executed depending on the if condition result, they will be printed to Log. You can test this by following two short scripts:

 

 

a = 1; //will print 1 to Log as this is the only and last row in script

 

a = 1;
Write(); //will print this as it is the last row of code

And if you did mean how ex variable works, you can fairly easily check it by running the example in scripting index and add Show(ex) in the end.

Below is an example with the if statement made a bit more clear by adding brackets and comments and I also added Show() for ex variable:

 

Names Default To Here(1);
If(
	( //condition1 starts
		ex = New Window("Dialog() example",
			<<Modal,
			<<Return Result,
			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
	), //condition1 ends
		ex["variable"]; //result1
	, //ELSE
		"CANCEL" //returnElse
);
Show(ex);

(note due to the Show() "CANCEL" isn't the last line of code when Cancel is pressed and it won't get printed, this will also show you that the variable value will be updated even if cancel is pressed)

Hopefully I did explain everything correctly. You can find more info from JMP Help and Scripting Guide: Construct a Modal Window 

 

 

-Jarmo
ozs02
Level III

Re: Force User to Provide Input Within Range

Thanks so much again, super helpful!

Re: Force User to Provide Input Within Range

You can use a Number Edit Box and set lower and upper bounds for the input, and even declare if the bounds are valid or not. You do not need to write an expression or a function to check. It is built into this display box already.

jthi
Super User

Re: Force User to Provide Input Within Range

Hadn't seen this earlier (or have and have forgotten about it). Using << Set Minimum and << Set Maximum:

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 Minimum(1), << Set Maximum(4))
	),
);
input = w["str1"];
show(input);
-Jarmo