Skip to main content
JMFreeman
Inspiring
January 4, 2022
Answered

Handling User Input With app.response

  • January 4, 2022
  • 1 reply
  • 1858 views

I am developing a form in which I want a user to interact using a dialog box. The intent is for the user to provide an integer. What is the best way to ensure/enforce that? Here is my code so far:

 

function updatePg(){
	var firstPhN = app.response({cQuestion:"What number should the first photo be?", cTitle:"First Photo Number", cLabel:"Photo:"});
	if(parseInt(firstPhN) != "NaN"){
		firstPhN = parseInt(firstPhN);
	}else{
		app.alert("Please input an integer.");
		firstPhN = 1;
	}
	for(var p = 0; p < this.numPages; p++){
		if(getFieldOn("pgN", p) != undefined) getFieldOn("pgN", p).value = "Page  " + (p + 1) + " of ";
		this.getField("pgT").value = this.numPages;
		if(getFieldOn("topNum", p) != undefined) getFieldOn("topNum", p).value = "Photo " + ((p*2) + firstPhN) + " - ";
		if(getFieldOn("botNum", p) != undefined) getFieldOn("botNum", p).value = "Photo " + ((p + 1)*2 + firstPhN - 1) + " - ";
	}
}

 

Sorry for the readability.. some of my lines are long, and I'm not overly experienced in this space.

 

When I enter an integer, everyhing works correctly. When I enter letters, the form shows "NaN" in the relevant fields. Why is that? It seems to me that the issue is with my if not going to its else block.

 

I would like to understand how I can make this approach (using a response box) work. I have made a version where I don't use a rseponse box, and it works fine, although it has its own quirks. 

This topic has been closed for replies.
Correct answer try67

Okay, good info. Could you provide any more help than that?


Use the isNaN method, instead:

 

if (isNaN(parseInt(firstPhN))) {

1 reply

Bernd Alheit
Community Expert
Community Expert
January 5, 2022

Does you get the message from the alert?

JMFreeman
JMFreemanAuthor
Inspiring
January 5, 2022

No. The else portion never seems to trigger. Let me know if I need to attach a file. I might need to strip some stuff before I can do that if so.

try67
Community Expert
Community Expert
January 5, 2022

Use the isNaN method, instead:

 

if (isNaN(parseInt(firstPhN))) {


Addendum: You should also check if the user didn't enter anything, as an empty string will be parsed as zero, which is a valid number.