#1
  1. No Profile Picture
    Registered User
    ASP Explorer (0 - 99 posts)

    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Need help creating an error message


    The code is suppose to if an number is not entered by the user that an error message stating that they must enter a number and allows them to try again. Can anyone help me to figure out where and what code I need to add?

    <script type="text/javascript" charset="UTF-8">
    var width = prompt( "Enter the width of a rectangle." )
    if ( isNaN(width) ) {
    alert("You must enter a number.");
    width;
    }
    var height = prompt( "Enter the height of a rectangle." )
    if ( isNaN(height)){
    alert("You must enter a number.");
    height;
    }
    var area = parseInt(width) * parseInt(height);

    alert( "The area of the rectangle is " + area);
    </script>
  2. #2
  3. No Profile Picture
    Registered User

    Join Date
    Feb 2013
    Location
    Norfolk, UK
    Posts
    1
    Rep Power
    0
    One problem is that you left off semi-colons at the end of a couple of lines. Also, a couple of lines were redundant. But then that will only check that the user entered some text instead of a number. If you want to prevent them simply pressing "enter", you could check that the number isn't zero as well.
    Additionally, you'd presumably want to go back to the prompt until they'd entered a number. Something like this:

    Code:
    <script type="text/javascript" charset="UTF-8">
    var isValid = 0;
    while (isValid == 0) {
    	var width = prompt( "Enter the width of a rectangle." );
    	if ( isNaN(width) || width==0 ) {
    		alert("You must enter a number.");
    	} else {
    		isValid = 1;
    	}
    }	
    
    isValid = 0;
    while (isValid == 0) {
    	var height = prompt( "Enter the height of a rectangle." );
    	if ( isNaN(height) || height==0 ) {
    		alert("You must enter a number.");
    	} else {
    		isValid = 1;
    	}
    }	
    
    var area = parseInt(width) * parseInt(height);
    
    alert( "The area of the rectangle is " + area);
    </script>

Similar Threads

  1. Advice on creating email message
    By trevi in forum Microsoft Access Help
    Replies: 3
    Last Post: June 21st, 2012, 12:54 AM
  2. Problems when creating an object cdo.message
    By sisuka in forum ASP Development
    Replies: 4
    Last Post: April 7th, 2006, 02:33 AM
  3. creating a help message from a command button
    By ddennis in forum Microsoft Access Help
    Replies: 1
    Last Post: April 30th, 2005, 06:41 PM
  4. Creating Message Box in the FORM of MS Access 2002
    By szms in forum Microsoft Access Help
    Replies: 7
    Last Post: July 6th, 2004, 09:48 AM
  5. creating error message
    By Steve Schofield in forum ASP Development
    Replies: 2
    Last Post: December 15th, 2000, 03:44 AM

IMN logo majestic logo threadwatch logo seochat tools logo