Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8391879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:22:26+00:00 2026-06-09T19:22:26+00:00

I have written this small script. Can someone help me fix this I have

  • 0

I have written this small script. Can someone help me fix this

I have a problem in mathcing the values of the array against the user input from the textfield.

I am not sure, whether I need to use a for loop, but the logic seems to be very simple to achieve without for loops.

Thanks in advance

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">

var zone1 = new Array("london", "manchester", "spain", "paris");
var zone2 = new Array("newyork", "toronto", "los angeles", "vancouver");
var zone3 = new Array("delhi", "seoul", "moscow", "dhaka");

function validateForm()
{
var x=document.forms["myForm"]["fname"].value;

 if (x==zone1)
  {
  alert("You are in Europe");
  return false;
  }

  else if (x==zone2)
  {
  alert("You are in North America");
  return false;
  }

  else if (x==zone3)
  {
  alert("You are in Asia");
  return false;
  }

  else{
      alert("try again");
      return false;

  }




}
</script>
</head>

<body>
<form name="myForm" action="" onsubmit="return validateForm()" method="post">
Enter your City: <input type="text" name="fname">
<input type="submit" value="Submit">
</form>
</body>

</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T19:22:27+00:00Added an answer on June 9, 2026 at 7:22 pm

    Something like this should work (untested, but shows you the idea):

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript">
    
    var zone1 = new Array("london", "manchester", "spain", "paris");
    var zone2 = new Array("newyork", "toronto", "los angeles", "vancouver");
    var zone3 = new Array("delhi", "seoul", "moscow", "dhaka");
    
    var zones = {};
    var zoneLabels = {};
    
    zoneLabels.europe = "Europe";
    zoneLabels.northamerica = "North America";
    zoneLabels.asia = "Asia"; 
    
    zones.europe = zone1;
    zones.northamerica = zone2;
    zones.asia = zone3;
    
    
    function validateForm(){
        var x=document.forms["myForm"]["fname"].value;
    
        for (i in zones){
            var zone = zones[i];
            for(j in zone){
                if( x == zone[j]){
                    alert("you are in " + zoneLabels[i]);
                    return false;
                }
            }
        }
    
        alert("try again");
        return false;
    }
    </script>
    
    <title>test</title>
    </head>
    
    <body>
    <form name="myForm" action="" onsubmit="return validateForm()" method="post">
        Enter your City: <input type="text" name="fname">
        <input type="submit" value="Submit">
    </form>
    </body>
    
    </html>
    

    if you want to map the continent properties of the zones object to custom labels, you could define a second object (I edited the code above)

    UPDATE:

    If you want to add more zones, you should add a new zone array:

    var zone4 = new Array("canberra","perth");
    

    Add this to the zones object:

    zones.australia = zone4;
    

    And add an entry to the zoneLabels object:

    zoneLabels.australia = "Australia";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written (tried to) this small bash script for searching through a range
This may be a basic question. I have written a small perl script to
I already have written a login and register script. The user can login to
So I am obviously not a very good programmer. I have written this small
I have written this code what it does is if user types postcode or
I have written this short script (which I've stripped away some minor detail for
I have written a script which is basically a small wysiwyg signature generator for
Can we have multiple SQL* PLUS connections in a shell script? I have written
I am generating new xml file using perl script but I have small problem,
I have shell scripting knowledge. I have written a small shell script which will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.