JavaScript - Simplify Multiple (or) Statements
can someone see a way to minimize this down to one statement that makes it infinate?
if(x == i) || (x*1 == i) || (x*2 == i) || (x*3 == i) || (x*4 == i) || ............. x is a var user input. it is numeric. i is the counter placed above the function. Similar TutorialsHi, been using javascript for a while now, and wondering how else this could be coded to be more effective and to 'work'. The idea is that the user gets prompted, 'what is your birthday?' the user would put in 0121 or whatever their birthday is, as 01 = month and 21 = day. I'm sure you'll understand what I mean.... Code: <DOCTYPE html> <html> <script> var userAnswer = prompt("When is your birthday? Example: Jan 21st = 0121") </script> <head> </head> <body> <script> if(userAnswer >= 0101){ alert('You are Capricorn!') }; if(userAnswer >= 0120){ alert('You are Aquarius!') }; if(userAnswer >= 0219){ alert('You are Pisces!') }; if(userAnswer >= 0321){ alert('You are Aries') }; if(userAnswer >= 0420){ alert('You are Taurus') }; if(userAnswer >= 0521){ alert('You are Gemini!') }; if(userAnswer >= 0621){ alert('You are Cancer!') }; if(userAnswer >= 0723){ alert('You are Leo!') }; if(userAnswer >= 0823){ alert('You are Virgo!') }; if(userAnswer >= 0923){ alert('You are Libra!') }; if(userAnswer >= 1023){ alert('You are Scorpio!') }; if(userAnswer >= 1122){ alert('You are Sagittarius!') }; if(userAnswer >= 1222){ alert('You are Capricorn!') }; </script> </body> </html> Reply With Quote 01-18-2015, 10:01 AM #2 Mitchellwood101 View Profile View Forum Posts New to the CF scene Join Date Nov 2014 Posts 7 Thanks 0 Thanked 0 Times in 0 Posts And then it will alert the user of their star sign****** Reply With Quote 01-18-2015, 05:47 PM #3 Philip M View Profile View Forum Posts Supreme Master coder! Join Date Jun 2002 Location London, England Posts 18,371 Thanks 204 Thanked 2,573 Times in 2,551 Posts If you have been using Javascript for a while now, you should be aware that prompts and alerts are long obsolete, and I would advise that you should not be wasting your time learning this stuff. Use DOM methods to obtain input from and display messages to your users. Be aware that a value starting with 0 is a string, not a number, and so must be in quotes - that is why your code does not work. In any case you need rigorous checking to ensure that the birthday entered in this form is valid, e.g. not 56th Febtember or "Mickey Mouse". You would do best to obtain the input from select lists for month and day, expressed as numbers. A tiny bit more sophistication would allow the user to select a month by name, where the text of the option is the month name, and the value of that option the month number. Code: <select id = "monthlist" onchange = "getSignFunction()"> <option value = "0"> Choose your birth month</option> <option value = 1>January</option> <option value = 2>February</option> ... and so on </select> Then you want something like this:- Code: function getSignFunction()"{ var sign = ""; var month = document.getElementById("monthlist").value; var date = document.getElementById("datelist").value; if (month == 0) {return} // invalid choice if (month == 1 && date >=20 || month == 2 && date <=18) {sign = "Aquarius";} if (month == 2 && date >=19 || month == 3 && date <=20) {sign = "Pisces";} if (month == 3 && date >=21 || month == 4 && date <=19) {sign = "Aries";} if (month == 4 && date >=20 || month == 5 && date <=20) {sign = "Taurus";} if (month == 5 && date >=21 || month == 6 && date <=21) {sign = "Gemini";} if (month == 6 && date >=22 || month == 7 && date <=22) {sign = "Cancer";} if (month == 7 && date >=23 || month == 8 && date <=22) {sign = "Leo";} if (month == 8 && date >=23 || month == 9 && date <=22) {sign = "Virgo";} if (month == 9 && date >=23 || month == 10 && date <=22) {sign = "Libra";} if (month == 10 && date >=23 || month == 11 && date <=21) {sign = "Scorpio";} if (month == 11 && date >=22 || month == 12 && date <=21) {sign = "Sagittarius";} if (month == 12 && date >=22 || month == 1 && date <=19) {sign = "Capricorn";} alert (sign); // alert used here just for testing purposes. Crate as <span> and then use document.getElementById("spanid").innerHTML = sign to display the result to the user. } All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit. I need to create javascript that will create the ringup line (see code) based on accomodation selections. If they chose Wednesday as arrival and depart Thursday they will get the breakdown_wed_supplement value. If they arrive Wednesday and depart later than Thursday they will get charged for Wed and Thursday and finally if they arrive Thursday, they will get charge for Thursday. The Friday - Sunday values are working, but I can't get the Wed/Thurs to work. They were written by someone else and were working, but I needed to change it for the Thursday departure. I am really new to javascript and haven't got a clue where I went wrong. Code: if (getControlValue(form.accommodation)==="Yes") { if (getControlValue(form.occupancy)==="Single") { total.ringup(conferencePrices.lodgingFeeConference,'Conference lodging for single person, Friday through Sunday','breakdown_lodging'); } else if (getControlValue(form.occupancy)==="Double") { total.ringup(conferencePrices.lodgingFeeConferenceRoommate,'Conference lodging with roommate, Friday through Sunday','breakdown_lodging'); } if (form.arrivedate.selectedIndex===1 && form.departdate.selectedIndex===2) { if (getControlValue(form.roommate)==="Assign" || form.rm_arrive.selectedIndex===1 && form.rm_depart.selectedIndex===2) { total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Wednesday pre-conference lodging, with roommate','breakdown_wed_supplement'); } else { total.ringup(conferencePrices.lodgingFeeNightly,'Wednesday pre-conference lodging, single person','breakdown_wed_supplement'); } } if (form.arrivedate.selectedIndex===1 && form.departdate.selectedIndex===>2) { if (getControlValue(form.roommate)==="Assign" || form.rm_arrive.selectedIndex===1 && form.rm_depart.selectedIndex===>2) { total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Thursday pre-conference lodging, with roommate','breakdown_thurs_supplement'); total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Wednesday pre-conference lodging, with roommate','breakdown_wed_supplement'); } else { total.ringup(conferencePrices.lodgingFeeNightly,'Thursday pre-conference lodging, single person','breakdown_thurs_supplement'); total.ringup(conferencePrices.lodgingFeeNightly,'Wednesday pre-conference lodging, single person','breakdown_wed_supplement'); } } if (form.arrivedate.selectedIndex===2) { if (getControlValue(form.roommate)==="Assign" form.rm_arrive.selectedIndex===2) { total.ringup(conferencePrices.lodgingFeeNightlyRoommate,'Thursday pre-conference lodging, with roommate','breakdown_thurs_supplement'); } else { total.ringup(conferencePrices.lodgingFeeNightly,'Thursday pre-conference lodging, single person','breakdown_thurs_supplement'); } } } I have an order form which is validated by javascript... To validate the fields I have used multiple if statements! But the problem is it works well when checking the fields from top to bottom; but if the user leaves all the upper fields blanks and only inputs the last field, the script fails to validate all the upper fields.. Please help me improve my javascript... The javascript code is validatecalculate.js Code: function setFocus() { document.getElementById("fname").focus() } function IsNumeric(strString) // check for valid numeric strings { var strValidChars = "0123456789 ";//allowed numeric and space var strChar; var blnResult = true; if (strString.length == 0) return false; // test strString consists of valid characters listed above for (i = 0; i < strString.length && blnResult == true; i++) { strChar = strString.charAt(i); if (strValidChars.indexOf(strChar) == -1) { blnResult = false; } } return blnResult; } function isNaN(strelem){ var numericExpression = /^[0-9]+$/; if(strelem.match(numericExpression)){ return true; }else{ return false; } } function validate_form() { missinginfo = ""; //alert("show hidden value " + (document.myform.totalsub.value)); if (document.getElementById("fname").value == "") { missinginfo += "\n - First Name Not Entered"; } if (document.getElementById("lname").value == "") { missinginfo += "\n - Last Name Not Entered"; } if ((document.getElementById("email").value.indexOf("@") < 1) || (document.getElementById("email").value.lastIndexOf(".") < 2)) { if (document.getElementById("email").value == "") { missinginfo += "\n - Email Address Not entered"; } else{ missinginfo += "\n - Invalid email Address.\n >>email should have the format x@abc.yy"; } } if (IsNumeric(document.getElementById("phone").value) == false) { if (document.getElementById("phone").value == "") { missinginfo += "\n - Phone Number Not entered"; } else{ missinginfo += "\n - Invalid Phone Number.\n >> Please enter numeric characters (space allowed)";} } var reg = /^\d{1,}\S\d{1,}\s[a-zA-Z]+\s[a-zA-Z]+$/; if (reg.test(document.getElementById("saddress").value)== false) { if (document.getElementById("saddress").value== "") { missinginfo += "\n - Street Address Not entered"; } else{ missinginfo += "\n - Invalid Street Address.\n >> Please use format 0/00 Tiger Street"; } } if (document.getElementById("suburb").value == "") { missinginfo += "\n - Suburb Not Entered"; } var pval = /^\d{4}$/; if (pval.test(document.getElementById("pcode").value)== false){ if (document.getElementById("pcode").value== "") { missinginfo += "\n - Postcode Not Entered"; } else{ missinginfo += "\n - Invalid Post Code.\n >> Please enter four numbers of your postcode"; } } if (document.getElementById("ccname").value == "") { missinginfo += "\n - Name on Credit Card not entered"; } cval = /^\d{16}$/; if (cval.test(document.getElementById("ccnum").value)== false){ if (document.getElementById("ccnum").value == "") { missinginfo += "\n - Credit Card Number Not entered"; } else { missinginfo += "\n - Invalid Credit Card Number.\n >> Please enter 16 digits on your credit card. Ignore the dashes and spaces."; } } ymval=/^\d{2}$/; if (ymval.test(document.getElementById("edatemonth").value)== false) { if (document.getElementById("edatemonth").value == "") { missinginfo += "\n - Credit Card Expiry Month Not Entered"; } else{ missinginfo += "\n - Invalid Expiry Month.\n >> Please enter 01 for Jan, 02 for Feb.... 11 for Nov & 12 for Dec."; } } if (ymval.test(document.getElementById("edateyear").value)== false){ if (document.getElementById("edateyear").value == "") { missinginfo += "\n - Credit Card Expiry Year Not Entered"; } else{ missinginfo += "\n - Invalid Expiry Year.\n >> Please enter last two digits only. e.g.: 12 for 2012"; } } cvvval=/^\d{3}$/; if (cvvval.test(document.getElementById("cvv").value)== false){ if (document.getElementById("cvv").value == "") { missinginfo += "\n - CVV Not Entered"; } else{ missinginfo += "\n - Invalid CVV.\n >> Please enter the 3 digits only"; } } else return true; if (missinginfo != "") { missinginfo ="______________________________________________\n" + "Form not Submitted:\n" + missinginfo + "\n______________________________________________" + "\nPlease check and submit again!"; alert(missinginfo); return false; } else return true; } function CalculateTotal() { var bolt = parseInt(document.getElementById('qnty1').value); var nut = parseInt(document.getElementById('qnty2').value); var washer = parseInt(document.getElementById('qnty3').value); document.getElementById("tot1").value = (bolt * 2.15).toFixed(2); document.getElementById("tot2").value = (nut * 0.45).toFixed(2); document.getElementById("tot3").value = (washer * 0.30).toFixed(2); document.getElementById("totqnty").value = ((bolt) + (nut) + (washer)); document.getElementById("totamount").value = ((bolt * 2.15) + (nut * 0.45) + (washer * 0.30)).toFixed(2); } The html code with the form is Code: <?xml version = "1.0" encoding = "utf-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- order.html The order page for 'Nuts 'R' Us' --> <html xmlns = "http://www.w3.org/1999/xhtml"> <head> <link rel="stylesheet" type="text/css" href="task2.css" /> <script type="text/javascript" src="validatecalculate.js"> </script> <title>Nuts 'R' Us - Order</title> </head> <body onload="return setFocus();"> <table class="table"> <tr><td><h1> Nuts 'R' Us </h1></td></tr> <tr><td class="text2"> The best store in town.</td></tr> <tr><td> <table class="table2"> <tr><td class="td1"><a href = "main.html"> Home </a></td><td></td> <td class="td1"> <a href = "order.html">Order </a></td><td></td> <td class="td1"> <a href = "http://csusap.csu.edu.au/cgi-pub/rgauta01/process.cgi">Shopping-Cart </a></td></tr> </table></td> </tr> <tr><td> <hr /> </td></tr> <tr><td><form action = "http://csusap.csu.edu.au/cgi-pub/rgauta01/process.cgi" method="post" id="myform"> <table class="table"> <tr><td>First Name:</td> <td><input type = "text" name ="fname" id="fname" /></td></tr> <tr><td>Last Name:</td> <td><input type = "text" name ="lname" id="lname" /></td></tr> <tr><td>email:</td> <td><input type = "text" name ="email" id="email" /></td></tr> <tr><td>Phone:</td> <td><input type = "text" name ="phone" id="phone" /></td></tr> <tr><td>Street Address:</td> <td><input type = "text" name ="saddress" size ="50" id="saddress" /></td></tr> <tr><td>Suburb:</td> <td><input type = "text" name ="suburb" size ="30" id="suburb" /></td></tr> <tr><td>Postcode:</td> <td><input type = "text" name ="pcode" size ="30" id="pcode" /></td></tr><tr><td colspan="2" class="text3">Payment Details</td></tr> <tr><td colspan="2">Type of card: <input type="radio" name="cardtype" value="visa" id="visa" checked="checked" />Visa <input type="radio" name="cardtype" value="mastercard" id="mastercard" />Mastercard <input type="radio" name="cardtype" value="amex" id="amex" />Amex <input type="radio" name="cardtype" value="diners" id="diners" />Diners</td></tr> <tr><td>Name on Credit Card:</td> <td><input type = "text" name ="ccname" size ="50" id="ccname" /></td></tr> <tr><td>Credit Card Number</td> <td><input type = "text" name ="ccnum" maxlength ="16" size ="50" id="ccnum" /></td></tr> <tr><td colspan="2" class="text3">Expiry Date</td></tr> <tr><td>Month(MM):</td> <td><input type = "text" name ="edatemonth" size ="5" maxlength ="2" id="edatemonth" /></td></tr> <tr><td>Year(YY):</td> <td><input type = "text" name ="edateyear" size ="5" maxlength ="2" id="edateyear" /></td></tr> <tr><td>CVV:</td> <td><input type = "text" name ="cvv" size ="5" maxlength ="3" id="cvv" /></td></tr> <tr><td colspan="2" class="text3">Order Details</td></tr> <tr><td colspan="2"><table class="table3"> <tr><th>Item</th> <th>Product Code</th> <th>Diameter</th> <th>Length</th> <th>Colour</th> <th>Unit</th> <th>Quantity</th> <th>Price($)</th></tr> <tr><td>Bolt</td><td>B113</td> <td>9</td><td>50</td> <td>Black</td> <td>2.15</td> <td class="td1"><input type ="text" name ="qnty1" size="3" onchange="CalculateTotal()" id="qnty1" value="0" /></td> <td class="td1"><input type ="text" name ="tot1" size ="3" id="tot1" value="0.00" readonly="readonly" /></td></tr> <tr><td>Nut</td> <td>N234</td> <td>5</td> <td>N/A</td> <td>Silver</td> <td>0.45</td> <td class="td1"><input type ="text" name ="qnty2" size ="3" onchange="CalculateTotal()" id="qnty2" value="0" /></td> <td class="td1"><input type ="text" name ="tot2" size ="3" id="tot2" value="0.00" readonly="readonly" /></td></tr> <tr><td>Washer</td> <td>W359</td> <td>8</td> <td>N/A</td> <td>Silver</td> <td>0.30</td> <td class="td1"><input type ="text" name="qnty3" size ="3" onchange="CalculateTotal()" id="qnty3" value="0" /></td> <td class="td1"><input type ="text" name ="tot3" size ="3" id="tot3" value="0.00" readonly="readonly" /></td></tr> <tr><td colspan = "6" class="td2">Total: </td><td class="td1"><input type ="text" name ="totqnty" size ="3" id="totqnty" value="0" readonly="readonly" /></td> <td class="td1"><input type ="text" name ="totamount" size ="3" id="totamount" value="0.00" readonly="readonly" /></td></tr> </table></td></tr> <tr><td colspan="2" align="center"><input type = "reset" value = "Reset" /> <input type = "submit" value = "Submit" name="submit" onclick="return validate_form();"/></td></tr></table> </form> </td> </tr> <tr><td colspan="2"> <a href="http://validator.w3.org/check?uri=referer"><img src="http://www.w3.org/Icons/valid-xhtml10" alt="Valid XHTML 1.0 Strict" height="31" width="88" /></a> </td></tr> <tr><td class="td1"> <hr /> © Nuts 'R' Us - 2011. All Rights Reserved. </td> </tr> </table></body> </html> My problem is that i have multiple math calculations within different functions that have multiple switch statements that get calculated with an onclick="functionOne()" for each radio button and before i can calculate the outcome for some reason i must fully insert a value for every input as well as check every raidio button before i get to the end of my list. I wish to simply allow the user to fill out as many fields as he wishes leaving some bank without being restricted to do every single one.. Thank you in advance
My crumby DVR (security dvr not tivo) has a remote access page that will run ONLY on internet explorer because it uses active x. This means I can't run it on any other awesome browser and no phones . I want to re write it a little bit, all I need is the log in so I need to re write this a little bit so it will work on all browsers once that happens i can port it to my phone. the video stream comes unprotected and can be viewed decently through its RTSP I just can't control anything. My thoughts are to make a simple webpage with the video streams coming in via RTSP and when you log in you can send commands and can muk about the settings its just the java uses active x and theres a weird server side script I can't make heads or tails of. Here is my log on page, for a limited time I set it back to defaults... http://67.85.223.199:3001 Username: ADMINISTRATOR Password: 000000 so just view the source you don't even have to be in IE to view the source and look at the amount js files. I don't need any of the languages obviously and I don't need to set schedules or any settings, just need the PTZ (pan tilt zoom) controls to work and I need to be logged into do that, I just cant figure out how to make the log in when i re write it. Feel free to mess around I unhooked all cameras except for one, the only thing I ask is that someone look at the source code and take a look at the js and help me make heads/tails of what I don't need I want to make just a basic log in! ohh if you want to just stream the video through RTSP then the RTSP link in rtsp://67.85.223.199:110/cgi-bin/rtspStream/1 ohh and the DVR is a Bosch 600 series a 650-16A to be precise! Don't care what you do, this thing is useless to me if I can't CONTROL it from my phone like they told me. What's the best way to go about this? -Mike hi guys, can someonr put this into an array for me please--don't know much about js.. much obliged for your help btw...when I call the site from google, the divs are open but when I call the site from my browser address bar...the divs are hidden as should be (only in firefox, otherwise great. Any ideas why that might be) thanks a ton DD Code: jQuery(document).ready(function() { jQuery(".content").hide(); //toggle the componenet with class msg_body jQuery(".headinga").click(function() { jQuery(this).next(".content").slideToggle(500); }); }); jQuery(document).ready(function() { jQuery(".content").hide(); //toggle the componenet with class msg_body jQuery(".headingb").click(function() { jQuery(this).next(".content").slideToggle(500); }); }); jQuery(document).ready(function() { jQuery(".content").hide(); //toggle the componenet with class msg_body jQuery(".headingc").click(function() { jQuery(this).next(".content").slideToggle(500); }); }); SO, thanks for checking this out. If i have 3 text areas for output and an array that can be of size 0 to however big a user passes in values. How would I display the last 3 values of the array in the text areas? Here's what I was thinking psuedoCode, but I just want to know if there is an easier way. thnx! Code: //handling all the cases (ie if i have less than 3 items) var size = array.length display 0 if(size <= 3) if(size == '3'){ display0 = array[0] display1 = array[1] display2 = array[2] if(size == '2') do the same but copy the 2 elements if(size == '1') do the same else var newSize = (size -1) - 1 //to find where the first of the 3 elements will be display0 = array[newsize] display1 = array[newsize+1] display2 = array]newsize+2] Its seems a bit extraneous doing it this method, so i was just wondering if anyone could think of a better way. Thanks again! Hi guys, Is there a way to simplify/shorten this already small piece of code? Ternary is the only thing I can think of, but it will only complicate the code. I don't believe a switch statement would apply. Code: if (sourceCode.substr(startStatus, 60).indexOf("Open") !== -1){ discStatus[i] = "OPEN"; } else if (sourceCode.substr(startStatus, 60).indexOf("Closed") !== -1){ discStatus[i] = "CLOSED"; } else if (sourceCode.substr(startStatus, 60).indexOf("W-List") !== -1){ discStatus[i] = "WAITLISTED"; } else if (sourceCode.substr(startStatus, 60).indexOf("Cancelled") !== -1){ discStatus[i] = "CANCELLED"; } Basically, there's a small substring that I'm checking for one of the four values (open, closed, w-list, and cancelled). Thanks for your time! Im trying to figure out why this code won't work. Unsuccessfully. Sorry, didn't know how to use the "CODE" tag. <html> <head> <script type:"text/javascript"> var BGColor = prompt("Would you like the background of the page to be red, green, or blue?","") </script> </head> <body> <script type:"text/javascript"> if (BGColor == "red") { document.write('<body bgcolor= "red">The body of this page is RED. Press F5 to restart!') } else if (BGCOlor == "green") { document.write('<body bgcolor= "green">The body of this page is GREEN. Press F5 to restart!') } else if (BGColor == "blue") { document.write('<body bgcolor= "blue">The body of this page is BLUE. Press F5 to restart!') } </script> </body> </html> Here is the code, in full: <htmll"> <head><title></title> <script type="text/javascript"> <!-- HIDE FROM INCOMPATIBLE BROWSERS function checkGrade(grade, value) { switch (grade) { case "A": if(grade == '90') window.alert("Your grade is excellent."); break; case "B": window.alert("Your grade is good."); break; case "C": window.alert("Your grade is fair."); break; case "D": window.alert("You are barely passing."); break; case "F": window.alert("You failed."); break; } } // STOP HIDING FROM INCOMPATIBLE BROWSERS --> </script> </head> <body> <form name="gradeForm"action="Your Grades"> <input type="text" name="grade" /> <input type="button" value="Check Grade" onclick="checkGrade(document.gradeForm.grade.value);" /> </form> </body> </html> What's throwing me off is the "A" in the case. As well, "(document.gradeForm.grade.value)" I need to make it a IF statement, but not sure how to call that function. The issue I am having is with my sumAll function: Code: <script type="text/javascript"> /* <![CDATA[ */ function addNumbers() { a = parseInt(document.getElementById('txtFirst').value); b = parseInt(document.getElementById('txtSecond').value); c = parseInt(document.getElementById('txtThird').value); d = parseInt(document.getElementById('txtFourth').value); ansE = document.getElementById("Sum"); ansE.value = '$' + (a + b + c + d); x = (a + b + c + d); if ( x >= 100000) { document.getElementById('Bonus').value = '$' + 1200; } else if ( x > 50000 && x<100000) { document.getElementById('Bonus').value = '$' + 800; } else { document.getElementById('Bonus').value = '$' + 0; } if (document.getElementById('Blue').checked == true && document.getElementById('Gala').checked == true) { document.getElementById('Committee').value = '$' + 800; } else if (document.getElementById('Blue').checked == true && document.getElementById('Gala').checked == false) { document.getElementById('Committee').value = '$' + 350; } else if (document.getElementById('Blue').checked == false && document.getElementById('Gala').checked == true) { document.getElementById('Committee').value = '$' + 450; } else { document.getElementById('Committee').value = '$' + 0; } } function sumAll() { y = parseInt(document.getElementById('Bonus').value); z = parseInt(document.getElementById('Committee').value); total = document.getElementById("Total"); total.value = (y + z); } function funcCalc() { addNumbers(); sumAll(); } /* ]]> */ </script> I'm really new to this and I'm trying to figure out how to sum the two values from if statements. ParseInt is returning NaN. Before that, it was adding my results together as strings. Is there an easy way to add these together without creating a very complicated if statement to total these up? Thanks! Code: <html> <script> // Author: L Freeman // Date: 11/10/10 // Purpose: Calculate student discount/vat reductions var price = 0; var vat = 17.5; var discount = 10; var reduction = 0; var deduction = 0; var reductionVAT = 0; var priceDiscount = 0; var priceNoVATDiscount = 0; // Calculate new price price = prompt("Enter price:"); // Calculate reduction reduction = (price/100) * discount; priceDiscount = (reductionVAT-reduction); priceNoVATDiscount = (price - reduction); // Calculate vat deduction deduction = (price/100) * vat; reductionVAT = (price - deduction); // Is VAT and/or a discount required? var answer = confirm ("VAT required?") if (answer) { alert ("Price = " + price + ", VAT = " + deduction + ", New Price is: " + reductionVAT) var answer = confirm ("Discount Required?") if (answer) { alert ("Price = " + reductionVAT + ", Discount = " + reduction + "New price is: " + priceDiscount) else alert ("No Discount Required. Price is: " + reductionVAT) } else alert ("No VAT required.") var answer = confirm ("Discount Required?") { if (answer) alert ("Price = " + price + ", Discount = " + reduction + "New price is: " + priceNoVATDiscount) else alert ("No Discount Required. Price is: " + price) } } </script> </html> I am having trouble getting this to work properly. What I am trying to do is come up with a simple vat deduction + student discount calculator. I cannot seem to get it working. Okay, so I got this code originally from another site. (A few months ago) I don't remember the site, and I was wondering if anyone could help me get it back how it was. Originally I believe the if statements were like this: Code: if (hr == 6, 7, 8) document.write("") I want the same code to appear when any of these numbers appear. Here is my current code: Code: document.write("<center><font size=+1>") day = new Date() hr = day.getMinutes() document.write("") if (hr == 0) document.write("<script type='text/javascript' src='http://www.1800banners.com/adserver/adserver.js'></script> <script type='text/javascript'> var varsarray=[]; varsarray[0]='24047'; if(!token) {var token='0'} else {var token=token+1;} var loadtype='0'; var adtype='0'; var background_color='#9C9C9C'; var border_color='#9C9C9C'; var text_color='#060606'; var text_rollcolor='#000000'; var domain_color='#000000'; varsarray['interhours']=''; varsarray['interpages']=''; adserver_initialize(token,loadtype,adtype,background_color,border_color,text_color,text_rollcolor,domain_color,varsarray); </script>") if (hr == 1) document.write("") if (hr == 2) document.write("") if (hr == 3) document.write("") if (hr == 4) document.write("") if (hr == 5) document.write(") if (hr == 6) document.write("") if (hr == 7) document.write("") if (hr == 8) document.write("") if (hr == 9) document.write("") if (hr == 10) document.write("") if (hr == 11) document.write("") if (hr == 12) document.write("") if (hr == 13) document.write("") if (hr == 14) document.write("") if (hr == 15) document.write("") if (hr == 16) document.write("") if (hr == 17) document.write("") if (hr == 18) document.write("") if (hr == 19) document.write("") if (hr == 20) document.write("") if (hr == 21) document.write("") if (hr == 22) document.write("") if (hr == 23) document.write("") if (hr == 24) document.write("") if (hr == 25) document.write("") if (hr == 26) document.write("") if (hr == 27) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 28) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 29) document.write("") if (hr == 30) document.write("<script type='text/javascript' src='http://www.1800banners.com/adserver/adserver.js'></script> <script type='text/javascript'> var varsarray=[]; varsarray[0]='24047'; if(!token) {var token='0'} else {var token=token+1;} var loadtype='0'; var adtype='0'; var background_color='#9C9C9C'; var border_color='#9C9C9C'; var text_color='#060606'; var text_rollcolor='#000000'; var domain_color='#000000'; varsarray['interhours']=''; varsarray['interpages']=''; adserver_initialize(token,loadtype,adtype,background_color,border_color,text_color,text_rollcolor,domain_color,varsarray); </script>") if (hr == 31) document.write("") if (hr == 32) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 33) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 34) document.write("") if (hr == 35) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=7&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 36) document.write("") if (hr == 37) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 38) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 39) document.write("") if (hr == 40) document.write("<script type='text/javascript' src='http://adhitzads.com/138517'></script>") if (hr == 41) document.write("") if (hr == 42) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 43) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 44) document.write("") if (hr == 45) document.write("<script type='text/javascript' src='http://adhitzads.com/136216'></script>") if (hr == 46) document.write("") if (hr == 47) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 48) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 49) document.write("") if (hr == 50) document.write("<SCRIPT LANGUAGE='JavaScript1.1' SRC='http://bdv.bidvertiser.com/BidVertiser.dbm?pid=244169&bid=856524' type='text/javascript'></SCRIPT>") if (hr == 51) document.write("") if (hr == 52) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 53) document.write("<script type='text/javascript'> clicksor_enable_adhere = false; clicksor_default_url = ''; clicksor_banner_border = '#99CC33'; clicksor_banner_ad_bg = '#FFFFFF'; clicksor_banner_link_color = '#000000'; clicksor_banner_text_color = '#666666'; clicksor_banner_image_banner = true; clicksor_banner_text_banner = true; clicksor_layer_border_color = ''; clicksor_layer_ad_bg = ''; clicksor_layer_ad_link_color = ''; clicksor_layer_ad_text_color = ''; clicksor_text_link_bg = ''; clicksor_text_link_color = ''; clicksor_enable_text_link = false; </script> <script type='text/javascript' src='http://ads.clicksor.com/showAd.php?nid=1&pid=154466&adtype=2&sid=233215&zone=13447'></script> <noscript><a href='http://www.yesadvertising.com'>affiliate marketing</a></noscript>") if (hr == 54) document.write("") if (hr == 55) document.write("<form action='https://www.paypal.com/cgi-bin/webscr' method='post'><input type='hidden' name='cmd' value='_s-xclick'> <input type='hidden' name='hosted_button_id' value='8220588'> <input type='image' src='https://www.paypal.com/en_US/i/btn/btn_donateCC_LG.gif' border='0' name='submit' alt='PayPal - The safer, easier way to pay online!'> <img alt='' border='0' src='https://www.paypal.com/en_US/i/scr/pixel.gif' width='1' height='1'> </form>") if (hr == 56) document.write("") if (hr == 57) document.write("") if (hr == 58) document.write("") if (hr == 59) document.write("") document.write("</font></center>") Any help? Or do you need more information? edit:: got it to work setting inBooth to false in the function where I clear the menu, but then it's refreshing to div so often while you're standing in the booth (30 frames/sec, it's a game) that nothing happens when you click the buttons to buy/sell unless you click really really fast.... anyone know a good way to check whether something is true, then toggle an innerHTML div, then once the value becomes false, toggle the innerHTML div to empty....??simply?? But if I reverse the order the opposite works but then the other doesn't...... Code: if (condition==true ) { generateSale(); inBooth=true; smithShop(); } else {inBooth=false;clearSale();} //IS PLAYER IN VENDOR BOOTH? if (condition2==true ) { generateSale(); inBooth=true; vendorShop(); } else {inBooth=false;clearSale();} Only the "generateSale()" from the second one executes. If I switch the if blocks, the second one with "generateSale()" executes. If I change "clearSale();" to a "setTimeout=('clearSale()',1000)" then both execute "generateSale()" but the div it affects flickers repeatedly as it's cleared/remade. generateSale() changes the innerHTML of a div, clearSale() just overwrites the innerHTML with ' ' Code: var sales2; function clearSale() { sales2=document.getElementById('sales2'); sales2.innerHTML=''; } function generateSale(){ sales2=document.getElementById('sales2'); sales2.innerHTML= 'Buy/Sell: '+' Gold coins: '+playerInventory[lootArray[7]]+'<br>' +' Consumables:<br>' +'You have: '+playerItems[itemArray[4]] +' arrows<br>' +'<input type="button" value="Buy" onclick="buyIt(0);">'+'10 for 50gp<br>' +'<input type="button" value="Sell" onclick="sellIt(0);">'+'10 for 25gp<br>' } Any ideas? Basically when my character enters the range of an image of a building I have a menu pop up saying you're in a vendor booth. I want it to run generateSale() which changes a div on the screen to list a bunch of stuff you can buy/sell. When your character is no longer standing in range, I want it to clear that information, so you have to be standing within range in order to trade items. Currently I can make it generates the sale menu, but it doesn't clear it when you leave the range. I've tried all kinds of "toggle" variables to set whether you're in range or not, but only the second block fires, not the first and I cannot figure out why... Any ideas? The code all works fine it just won't clear for all the different buildings, only one.... Trying to check validation on some dropdowns with this but man it's ugly. Is there a way to make it cleaner? Code: function checkDrop() { var sMorn = document.getElementById('wcsave_10_msession'); var sAft = document.getElementById('wcsave_10_asession'); var mMast = document.getElementById('wcsave_10_session_mmastery'); var aMast = document.getElementById('wcsave_10_session_amastery'); var mConc1 = document.getElementById('wcsave_10_session_mconc1'); var mConc2 = document.getElementById('wcsave_10_session_mconc2'); var aConc1 = document.getElementById('wcsave_10_session_aconc1'); var aConc2 = document.getElementById('wcsave_10_session_aconc2'); if(sMorn.options[sMorn.selectedIndex].value == 'Mastery session 1 - Computer Lab: Searching Essentials') { if ((sAft.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==1) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sAft.options[sAft.selectedIndex].value == 'Mastery session 2 - Beyond the Basics of Searching!') { if ((sMorn.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==1) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sMorn.options[sMorn.selectedIndex].value == 'Concurrent sessions 1-6') { if ((sAft.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==1) && (aConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sAft.selectedIndex ==2) && (aConc1.selectedIndex ==0) && (aConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } if(sAft.options[sAft.selectedIndex].value == 'Concurrent sessions 7-12') { if ((sMorn.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==1) && (mConc2.selectedIndex ==0)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } if ((sMorn.selectedIndex ==2) && (mConc1.selectedIndex ==0) && (mConc2.selectedIndex ==1)) { alert('Choose either a mastery session OR 2 concurrent sessions for the morning AND afternoon.'); return false; } } return true; } This may be a basic programing problem but I am writing a function to validate a form using a bunch of else-if statements and encounters a problem when I use nested if/else-if statements. It will not continue on to the next else-if, after it returns false. Code: // to check each entry on the form and alert user if entry is invalid. function checkform(){ var checkssn = /(\d{3})-(\d{2})-(\d{4})/; var checkphone = /(\d{3})-(\d{4})/; var checkname = /[a-zA-Z]+(\.|,|\s)*/; var checkzip = /(^967|^968)\d{2}(-\d{4})?$/; // check to see if user have selected an Election if (!document.f1.elections[0].checked && !document.f1.elections[1].checked && !document.f1.elections[2].checked && !document.f1.elections[3].checked) { window.alert("Please select an Election!") return false; // check to see if user entered a valid SSN } else if ( checkssn.test(document.f1.ssn.value) == false){ window.alert("[1]. Please enter a valid social security number in the format ddd-dd-ddd"); return false; // check to see if user entered a valid home telephone number or business telephone number }else if ( document.f1.home_phone.value == '' && document.f1.business_phone.value == '') { window.alert("[4]. Please enter a Home or Business telephone number!") return false; } else if ( document.f1.home_phone.value != ''){ if (checkphone.test(document.f1.home_phone.value) == false){ window.alert("[4]. Please enter a valid home phone number in the format ddd-ddd"); return false; } } else if ( document.f1.business_phone.value != ''){ if ( checkphone.test(document.f1.business_phone.value) == false){ window.alert("[4]. Please enter a valid business phone number in the format ddd-ddd"); return false; } // check to see if user entered a valid Name }else if ( checkname.test(document.f1.lastname.value) == false){ window.alert("[5]. Last Name can only consist of letters, periods(.), commas(,) and spaces"); return false; }else if ( checkname.test(document.f1.firstname.value) == false){ window.alert("[5]. First Name can only consist of letters, periods(.), commas(,) and spaces"); return false; The problem occurs when it validates the phone numbers. When a valid number is entered, it will not move to the next else-if statement to validate the name. It's been years since I program in Java/C, so I'm a bit rusty. Any help is appreciated. -Alex Hi I need help to finish this code with using nested if statements A price of a ticket to a passenger will be: First Class 500 Economy Class (with meal) 400 Economy Class (without meal) 200 How I can write a JavaScript code according to the following specifications: a. Read the class that the passenger wants to travel on. b. If the class is the first class, print the price of ticket. c. If the class is the economy class, ask the user if he/she wants a meal on the flight. Then print the price of the ticket according to the response of the passenger. The program should simply accept one possible strings the user enters; it is not required to work for all possible inputs such as First, first, FIRST or Yes , yes, YES. This is the code which I have been trying { var inputNum = prompt("Enter the class you want\n first class?\neconomy? :"); if (isNaN(inputNum)) { if (inputNum.match(/first class/)) { document.write("<h1><center>your Ticket is 500<\center><\h1>"); } else { prompt("<h1>if you want a meal on the flight press OK <\h1>"); document.write("<h1>your ticket is 400<h1>,"); } } Give me an example for switch statements. I had to create one that tests value of parameter using a text string, I came up with this value Code: function createBar(partyType,percent){ switch(partyType) { case D: <td class='dem'></td> break; Should I use " or ' between the dem? I am using break statements. do all breaks should end with string or do i need the bracket? I hope this does not seem like i getting people to do homework for me again,, just want to verify if i did this area correctly? Hi, I am trying to detect a users browsers and output a different code based on browser. I found this code online that uses the object detection method and it works. When I document.write(BrowserDetect.browser) it outputs Chrome, (which is what I am using). The code can be found he http://www.quirksmode.org/js/detect.html So I can't understand why the following code does not work? It ouputs the "not" in the else statement. I believe that BrowserDetect is an object and the .browser at the end is a property of the object. Can one not make an object a variable? or make an object a string? oh and BTW there is a bunch of javascript that creates the browserdetect object that can be found on the site above, but I thought it would be too unwieldy to post here. Thanks. The following script outputs: Chrome Not Working <script type="text/javascript"> document.write(BrowserDetect.browser); var browser="BrowserDetect.browser"; if(browser=="Chrome") { document.write(" Working"); } else { document.write(" Not working"); } </script> Hello, I have a long string of if..else statements, and the same is used in multiple functions. How do a make a variable that I can call instead of copying 40 lines of code? Thanks! |