/    Sign up×
Community /Pin to ProfileBookmark

using If else if else statement getting error

I am newbie to javascript I trying to code conditions for currency change based on shipping country but getting error for all conditions.

Conditions

if the country is India and currency is Indian Rupees – Proceed to next page
if the country is India and currency is not India Rupees – alert as “Change currency as Indian Rupees”
if the country is not India and currency is India Rupees – alert as “Change currency as other than Indian Rupees”
if the country is not India and currency is not Indian Rupees – Proceed to next page

Help me to correct the error.

Thanks in advance.

[CODE]<div id=”navMainSearch” style=”height: 50px; margin-top: 18px; margin-bottom: -15px;”>
<form method=”get” action=”” name=”currencies”>
<select id=”money” onchange=”” name=”currency”>
<option value=”USD”>US Dollar</option>
<option value=”EUR”>Euro</option>
<option value=”GBP”>British Pound Sterling</option>
<option value=”CAD”>Canadian Dollar</option>
<option value=”AUD”>Australian Dollar</option>
<option value=”INR”>Indian Rupees</option>
</select>
<input type=”hidden” value=”checkout” name=”main_page”>
<input type=”hidden” name=”fecaction”>
</form>
</div>
<div id=”checkoutShipto” class=”floatingBox back”>
<address class=”checkoutAddress”>
Raja
<br>
423, Vinayagar Kovil Street
<br>
Chennai, 600027
<br>
TN,India<div id=”location”>India</div>
</address>

</div>
<div class=”buttonRow forward”>
<input type=”submit” onclick=”submitFunction()” title=”Confirm Order ” alt=”Confirm Order” value=”submit” act>
</div>
</div>
<script type=”text/javascript”>
function submitFunction() {
var country = document.getElementById(“location”).innerText;
var currency = document.getElementById(“money”).innerText;
if (country == ‘India’ && currency == ‘INR’) {
window.location.assign(“thanks.html”);
} else if (country == ‘India’ && currency != ‘INR’) {
alert(“Change the currency as Indian Rupee “);
} else if (country != ‘India’ && currency == ‘INR’) {
alert(“Change the currency as other than Indian Rupee “);
} else {
window.location.assign(“thanks.html “);
}

}
</script>[/CODE]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@PadonakNov 18.2013 — <i>
</i>
function submitFunction(){
var country=document.getElementById("location").innerHTML,
sel=document.getElementById("money"),
currency=sel[sel.selectedIndex].value;
if(country == 'India' &amp;&amp; currency == 'INR'){window.location.assign("thanks.html");}
else if(country == 'India' &amp;&amp; currency != 'INR'){alert("Change the currency as Indian Rupee ");}
else if(country != 'India' &amp;&amp; currency == 'INR'){alert("Change the currency as other than Indian Rupee ");}
}

Copy linkTweet thisAlerts:
@MSPRajauthorNov 18.2013 — Thanks a lot.

Your Great Thanks again.......
Copy linkTweet thisAlerts:
@PadonakNov 18.2013 — there exist other (more flexible) ways of that check. if you have several countries and several currencies you can, for example, create the array which every member will look like:

['country',['currency_1','currency_2',etc.'currency_n']]

and then you'll run two loops. the first loop finds 'country' looking over the first members of each member. if it doesn't match the loop skips the member (continue), but if it matches the first member the second loop which looks over the second member (the array of acceptable currencies) starts. if the second loop finds the given currency it breaks and submits the form.

anyway, you must understand that the better check is done at the server-side
Copy linkTweet thisAlerts:
@jedaisoulNov 18.2013 — <i>
</i>
function submitFunction(){
var country=document.getElementById("location").innerHTML,
sel=document.getElementById("money"),
currency=sel[sel.selectedIndex].value;
if(country == 'India' &amp;&amp; currency == 'INR'){window.location.assign("thanks.html");}
else if(country == 'India' &amp;&amp; currency != 'INR'){alert("Change the currency as Indian Rupee ");}
else if(country != 'India' &amp;&amp; currency == 'INR'){alert("Change the currency as other than Indian Rupee ");}
}

[/QUOTE]

Just a small point. The above code works, but the test for "country != 'India'" is redundant. This is because, if the country IS 'India', one or other of the previous two tests MUST be true. So it can be simplified to:

else if(currency == 'INR'){alert("Change the currency as other than Indian Rupee ");}

Also, I'd echo the view that this test should be done server-side, as it is too easy for client-side validation to be bypassed. So you will NEED to validate server-side anyway. Validating both client and server-side is not a good idea because, if/when the criteria change, its very easy to forget that you need to amend two places! So you can get obscure errors because the client and server side validations differ.
×

Success!

Help @MSPRaj spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...