Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have this code that I was asked to write, and I've progressed a lot but have reached a dead end in this small part. So our specification says to have a drop down bar(a select option) which I have implemented as follows;

JavaScript
<label>State

    <select name="State">
        <option value="VIC">VIC </option>
        <option value="NSW">NSW</option>
        <option value="QLD">QLD</option>
        <option value="NT">NT </option>
        <option value="WA">WA </option>
        <option value="SA">SA </option>
        <option value="TAS">TAS </option>
        <option value="ACT">ACT</option>
    </select>

</label>


SO basically you choose a state, and then in the next field you need to type in your postal code. But,
The selected state must match the first digit of the postcode
VIC = 3 OR 8, NSW = 1 OR 2 ,QLD = 4 OR 9 ,NT = 0 ,WA = 6 ,SA=5 ,TAS=7 ,ACT= 0
(e.g. the postcode 3122 should match the state VIC)


And I have no idea how to go about doing this.

the next part of my code just looks like this;
JavaScript
<p>
					<label for="pcode">Post Code</label>
				
					<input id="pcode" type="text" name="pcode" size="4" />
					
				</p>


What I have tried:

I've tried multiple things such as creating a select option in front of the text box where I type in the postal code that dynamically changes the options within it based on the state I select but ran into issues because I'm still a novice at this. This is literally the last part of my assignment thats due in about 4 hours and I really could use some help! Thanks in advance guys!
Posted
Updated 13-Aug-18 14:22pm
v4
Comments
F-ES Sitecore 13-Aug-18 11:47am    
Even if you don't learn the solution to your problem I hope you've at least learned to not start your assignment 4 hours before it's due.
Member 13689608 13-Aug-18 12:17pm    
Ive had 2 weeks to write this, this is merely a part in the code that is like 5% of the entire code, and I just wanted to make sure that I have done all parts of this. Thanks for the feedback though, Im normally a very punctual person and this is the only part of the code that I havent done.
Richard MacCutchan 13-Aug-18 12:12pm    
So you need to read the post code value, and check the first character. You probably need some sort of array that matches the state ids to the post code digits. Probably an array of JavaScript Objects[^].

1 solution

few suggestion to your problem:
First of all, set number as value in your select tag
ie.
<SELECT>
 <option value="1"> first state </option>
 <option value="2"> second state </option>
 <option value="3"> third state </option>
</SELECT>


Now matching with first character with the try looking at String.indexOf function, you will bet

Alternative solution:
If you want to keep value as string, the way have currently declared, then you need to keep an data set that can represent this;
example:
// example 1
var stateResult = new Object();
stateResult['StateOne'] = 1;
stateResult['StateTwo'] = 2;
stateResult['StateThree'] = 3;

// example 2
var stateResult = {StateOne: 1, StateTwo: 2, StateThre: 3};

// read the data
console.log(stateResult.StateOne);
console.log(stateResult["StateTwo"]);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900