Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need Help Finding out problem with my code?! Please help!

    This Program is basically prompting the user to enter information such as name,address,province,city,country etc and then validating the information,
    I have two functions, one called promptInformation which takes in the name,address,province,city,country and postal code, this function basically prompts the user information, my other function validate information has the same parameters as prompt information, this function basically checks to see if the name,address,province,city,country and postal code are entered and if they are then it checks the country variable and if the country variable is canada it displays the name,address,province,city,country and postal code in red, and if its US it displays the information in blue.

    My problem is when the user does enter the correct information it isnt going into the else block? why is that? I changed the null to "" one time and it did go into the else block but it didnt go into the check for the country, so what is the problem here?

    Here is my code:
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    
    
    </head>
    
    <body>
    
    <script type="text/javascript">
    	
    	var name;
    	var country;
    	var province;
    	var city;
    	var streetAddress;
    	var postalCode;
    	
    	promptInformation(name,country,streetAddress,postalCode,city,province);
    	while(validation(name,country,streetAddress,postalCode,city,province)==0)
    	{
    		
    		promptInformation(name,country,streetAddress,postalCode,city,province);
    		
    	}
    	
    	function validation(name,country,streetAddress,postalCode,city,province)
    	{
        	
        	if(name==null ||name=="" || country==null||country=="" ||streetAddress==null||streetAddress=="" ||postalCode==null||postalCode=="" ||province==null||province=="")
        	{
    			return 0;
        	}
        	else
        	{
    			alert("blah");
        		if (country.toUpperCase()=="CANADA")
            	{
    				
            		document.write("<font color=red>"+name+"<br>"+streetAddress+"<br>"+city+","+province+","+country+"</font>");
    				return 1;
            	}
    			else if(country.toUpperCase()=="US")
    			{
    				document.write("<font color=blue>"+name+"<br>"+streetAddress+"<br>"+city+","+province+","+country+"</font>");
    				return 1;
    			}
    			else
    			{
    				return 0;
    			}
    		}
           
    
    
    }
    	function promptInformation(name,country,streetAddress,postalCode,city,province)
    	{
    		name=null;
    		country=null;
    		province=null;
    		city=null;
    		streetAddress=null;
    		postalCode=null;
    		 name=prompt("Please enter your name");
    		 country=prompt("Please enter your country");
    		 province=prompt("Please enter your province");
    		 city=prompt("Please enter your city");
    		 streetAddress=prompt("Please enter your street address");
    		 postalCode=prompt("Please enter your postal code");
    	
    	
    	}
    </script>
    
    
    
    </body>
    </html>
    Last edited by shivam1992; 01-20-2012 at 12:07 AM.

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,462
    Thanks
    23
    Thanked 634 Times in 633 Posts
    The WHILE statement makes this an infinite loop.

    Some points.
    The if(name==null ||name=="" || country==null......... will make this go back to entering info and the person entering info will not know why.
    You made the variables GLOBAL, you don't need to pass them into the functions.
    promptInformation(); will call the function and function promptInformation() and function validation() are good to go because they know the variables already.
    And also without looking at the code I wouldn't have known I needed to input us or US to get this to show blue.

    I suggest you evaluate each input as soon as it's entered if it's wrong or non existent tell the user why and ask for the input again. Then go into the red/blue printing.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •