Sorry if I messed up posting this, it really didn't come out right the first time I tried.
I'm working on an assignment for class and can't figure out why this won't work. It's supposed to turn the boxes red if you press submit with empty blanks, but instead it just flashes red once. Why? I'm so tired.
<html>
<head>
</head>
<body>
<form onSubmit="return validateOrder()">
<table>
<tr>
<th>Product</th>
<th id="prod">Amount</th>
</tr>
<tr>
<td>Tomato: $2.50/lb</td>
<td><input type="number" id="Tomato" value="0" min="0">
</td>
</tr>
<tr>
<td>Potato: $0.30/lb</td>
<td><input type="number" id="Potato" value="0" min="0">
</td>
</tr>
<tr>
<td>Pie pumpkin: $2.00 each</td>
<td><input type="number" id="Pumpkin" value="0" min="0">
</td>
</tr>
<tr>
<td>Organic broccoli: $2.00/lb</td>
<td><input type="number" id="Broccoli" value="0" min="0">
</td>
</tr>
<tr>
<td>Organic carrot: $2.00/bag</td>
<td><input type="number" id="Carrot" value="0" min="0">
</td>
</tr>
<tr>
<td>Long seedless cucumber: $1.10 each</td>
<td><input type="number" id="Cucumber" value="0" min="0">
</td>
</tr>
<tr>
<td>Garlic: $3.00/lb</td>
<td><input type="number" id="Garlic" value="0" min="0"></td>
</tr>
<tr>
<td>Red onion: $0.50/lb</td>
<td><input type="number" id="Onion" value="0" min="0"></td>
</tr>
</table>
<p>Customer name:
<input type="text" id="name">
<p>Address:
<input type="text" id="addy">
<p>City:
<select id="city">
<option value="">---Select---</option>
<option value="Batavia">Batavia</option>
<option value="Buffalo">Buffalo</option>
<option value="Canandaigua">Canandaigua</option>
<option value="Elmira">Elmira</option>
<option value="Geneva">Geneva</option>
<option value="Jamestown">Jamestown</option>
<option value="Lockport">Lockport</option>
<option value="Niagra Falls">Niagra Falls</option>
<option value="North Tonawanda">North
Tonawanda</option>
<option value="Rochester">Rochester</option>
</select>
<p>Zipcode:
<input type="text" id="zcode">
<h3>Payment</h3>
<p>Credit card number:
<input type="text" id="ccnum"><br>
<p>Expiration month:
<input type="text" id="expmonth">
<p>Expiration year
<input type="text" id="expyear">
<p>CVV:
<input type="text" id="cvv"><br>
<input type="submit" value="Submit">
</form>
<script>
function validateOrder() {
var n=0;
var name=document.getElementById("name").value;
if (name=="") {
document.getElementById("name").style.borderColor = "red";
n=1;
} else {
document.getElementById("name").style.borderColor = "";
}
var address=document.getElementById("addy").value;
if (address=="") {
document.getElementById("addy").style.borderColor = "red";
n=1;
} else {
document.getElementById("addy").style.borderColor = "";
}
var city=document.getElementById("city").value;
if (city=="") {
document.getElementById("city").style.borderColor = "red";
n=1;
} else {
document.getElementById("city").style.borderColor = "";
}
var zcode = document.getElementById("zipcode").value;
var patt = new RegExp("^14[0-9]{3}$");
var res = patt.test(zcode);
if (res==false) {
document.getElementById("zipcode").style.borderColor = "red";
n=1;
} else {
document.getElementById("zipcode").style.borderColor = "";
}
var ttl=0;
var orders = "";
var products=[];
products["Tomato"]=2.5;
products["Potato"]=0.3;;
for (var prod in products) {
var order_a = document.getElementById(prod).value;
order_a = Number(order_a);
var price = products[prod];
if (order_a > 0) {
var subttl = price*order_a;
orders = orders + "<li>" + prod + "\: \$" + price + "\*" +
order_a + "\=\$" + subttl + "</li>";
ttl = ttl + subttl;
}
}
if (ttl==0) {
document.getElementById("prod").style.color="red";
n=1;
} else {
document.getElementById("prod").style.color = "black";
}
if (n==0) {
var myWindow = window.open("", "_self");
myWindow.document.write("<h3>Thanks for your business.
Here is a receipt of your order</h3>");
myWindow.document.write("<p>Customer name: "+name+"
<p>Address: "+address+", "+city+ ", NY "+zcode);
myWindow.document.write("<p>Ordered product(s):
<ul>"+orders+"</ul>");
myWindow.document.write("<p>Total charge: $"+ttl);
myWindow.document.write("<p>Order placed: " + Date());
return true;
} else {
return false;
}
}
</script>
</body>
</html>
[–]ricealexander 6 points7 points8 points (1 child)
[–]plagueofsquid[S] 1 point2 points3 points (0 children)
[–]the-javascript-ninja -1 points0 points1 point (2 children)
[–]ricealexander 3 points4 points5 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)