Jump to content

Storing a value from a prompt box


matt4756
 Share

Recommended Posts

Hi,If I am using a javascript prompt box without a default text string in it, is it possible to ask the script to store a value if the box is left blank like if it is left blank then store the number 2? This script currently doesn't work due to the top bit. I can't seem to find how to store the number entered until I am ready to use it.If the box is blank, the script will return the message "You did not enter a number!" but then seems to end when ok is clicked. Is there a way to ask the script to re-run after this message?

<script type="text/javascript"><!--var number = prompt ("Please choose a number between 1 & 3")if (number==1)else if (number==2)else if (number==3)else if (number>3)alert ("You entered a number higher than 3!")else if (number<1)alert ("You entered a number less than 1!")elsealert ("You did not enter a number!")// --></script>

Link to comment
Share on other sites

Your selection structure is not valid. You have to tell the script what to do upon finding a match to what your checking. I didn't test the code, but did rewrite some of it to point you in the right direction. You may also have parse the numbers in the text string as integers using parseInt(). Everything that comes into your script from any textfield will be text. Including numbers. Not sure if JS compares them using ASCII characters.

<script type="text/javascript"><!--var number = prompt ("Please choose a number between 1 & 3")if ((number==1) ||	(number==2) ||	(number==3)) {instructions here}else if (number>3) {alert ("You entered a number higher than 3!");}else if (number<1) {alert ("You entered a number less than 1!");}else{alert ("You did not enter a number!");}// --></script>

Link to comment
Share on other sites

Here's a different take on the puzzle. We have several themes:1. Keep statements in functions as much as possible lest we devolve into spaghetti code. Notice that when we do this in Javascript, we can't call the function until it's literally been written, which is why the function call follows the function.2. Recursion is a powerful friend.3. An alert followed by a prompt is an icky user interface. With this in mind, I adjusted the warnings to be instructive rather than punitive.4. In response to your original question, I declared the 'number' variable as a global. It exists outside the function and will be available to any other function at any time.

	   <script type="text/javascript">	   	   function getNumber(myPrompt){		   number = prompt (myPrompt)	   		   if ((number == 1) ||			   (number == 2) ||			   (number == 3)) {				   //instructions here		   }		   else if (!number) { // my broswer treated blank as a zero, hence this special case			   getNumber ("You must enter a number.");		   }		   else if (number > 3) {			   getNumber ("Your number must be lower than 3.");		   }		   else if (number < 1) {			   getNumber ("Your number must be higher than 1.");		   }		   else{ // good practice to have a default; what if user enters a letter, etc?			   getNumber ("Please choose a number between 1 & 3");		   } 	   }	    var number;	   getNumber("Please choose a number between 1 & 3");	   	   	   </script>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...