Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-19-2012, 11:30 PM   PM User | #1
shivam1992
New to the CF scene

 
Join Date: Jan 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
shivam1992 is an unknown quantity at this point
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..
shivam1992 is offline   Reply With Quote
Old 01-20-2012, 05:14 AM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,383
Thanks: 18
Thanked 350 Times in 349 Posts
sunfighter is on a distinguished road
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.
sunfighter is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:07 PM.


Advertisement
Log in to turn off these ads.