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

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 10-11-2012, 02:59 AM   PM User | #1
AggressiveCoder
New to the CF scene

 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
AggressiveCoder is an unknown quantity at this point
Help Please with Javascript

I need help with this code. I have written it and the only part that works is the calculation of cost for the fruits. The other part where requires to validate the values does not work i.e if i put the values beyond 99 in textboxes, it is suppose to say..the values are suppose to be between 0-99, but it does not do that. Any help would be appreciated.

Thanks

Code:
<?xml version = "1.0" encoding = "utf-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org//TR/xhmtl/DTD/xhtml-strict.dtd ">
<html xmlns = "http:www.w3.org/1999/xhtml">
<head>
<title> nochange.html </title>
<script type = "text/javascript" src = "nochange.js" ></script>
<script>
function validateForm(){
  document.getElementById("Apple").onblur=function(){
  if(this.value==""){alert("Please Enter values");return}
  if(parseInt(this.value)<0 || parseInt(this.value)>99 ){alert("values should be betwween 0-99 ");}   
};

  document.getElementById("Orange").onblur=function(){
    if(this.value==""){alert("Please enter values");return}
     if(parseInt(this.value)<0 || parseInt(this.value)>99 ){alert("values should be between 0-99");}     
};

  document.getElementById("Banana").onblur=function(){
  if(this.value==""){alert("Please enter values");return}
   if(parseInt(this.value)<0 || parseInt(this.value)>99 ){alert("values should be between 0-99");}   
};

document.getElementById("btnTotalCost").onclick = function(){
        if( document.getElementById("Apple").value==""
            document.getElementById("Orange").value==""
            document.getElementById("Banana").value==""
        ){
            alert("Enter all the values properly");
        }    
    };               
}

</script>
</head>
<body>
<form action = "">
<h3> Fruits Shop </h3>
<table border = "border">
<tr>
<th> Product Name </th>
<th> Price </th>
<th> Quantity </th>
</tr>
<tr>
<th> Apple </th>
<td> $0.59 </td>
<td> <input type="text" id="Apple" size = "2" /> </td>
</tr>
<tr>
<th> Orange </th>
<td> $0.49 </td>
<td> <input type="text" id="Orange" size = "2" /> </td> 
</tr>
<tr>
<th> Banana </th>
<td> $0.39 </td>
<td> <input type="text" id="Banana" size = "2" /></td>
</tr>
</table>
<p>
<input type="button" id = "btnTotalCost" value = "Total Cost" onclick="computeCost();" />
<input type="text" size="5" id ="cost" onfocus="this.blur();" />
</p>
<p>
<input type = "Submit"  value = "Submit" />
<input type = "reset" value = "Clear" />
</p>

<script>
function computeCost() {
var apple = document.getElementById("Apple").value;
var orange = document.getElementById("Orange").value;
var banana = document.getElementById("Banana").value;
var totalCost = (apple * 0.59 + orange * 0.49 + banana * 0.39)*1.05;
document.getElementById("cost").value =totalCost;
}
</script>
</form>
</body>
</html>

Last edited by VIPStephan; 10-11-2012 at 10:19 PM.. Reason: added code BB tags
AggressiveCoder is offline   Reply With Quote
Old 10-11-2012, 03:12 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You never CALL the (VERY badly misnamed) function validateForm!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-11-2012, 03:13 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,247
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
And this code is total hash:
Code:
document.getElementById("btnTotalCost").onclick = function(){
    if( document.getElementById("Apple").value==""
    document.getElementById("Orange").value==""
    document.getElementById("Banana").value==""
    ){
        alert("Enter all the values properly");
    } 
};
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 10-11-2012, 07:49 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Do please read the posting guidelines regarding silly thread titles. The thread title is supposed to help people who have a similar problem in future. Yours is useless for this purpose. You can (and should) edit it to make it more meaningful.


BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

if(this.value==""){
What if the user enters a space or a letter or a ?

<input type = "Submit" value = "Submit" />
This does nothing but as you are not actually submitting a form to a server you should use <input type = "button"
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.

Last edited by Philip M; 10-11-2012 at 07:55 AM..
Philip M 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 02:04 AM.


Advertisement
Log in to turn off these ads.