JavaScript - Prompt And Alert Boxes With Numbers
Hi All,
I'm new to JS and need assistance. I'm trying to have a user input a number in a prompt box which gets totaled and displayed in an alert box. This is what I have and its not working out for me.. Any advice will be beneficial <html> <body> <script> var value1 = prompt("What is your first number?"); var value2 = prompt("What is your second number?"); { alert(value1 + value2); } </script > </body> </html> Similar TutorialsWhen you do an alert confirm you can make if statements to say if they click ok do this and if they click cancel stop the program. Well I am trying to make a prompt that first asks the user to enter in a url and after they do and click enter they url is wraped with img tags. I would like to make it so that if the user clicks cancel the program ends b/c right now if the user click okay with no value the result is [img][/img] and if the user clicks cancel the result is [img]null[/img]. Any step in the right direction would be greatly appreciated! PHP Code: <script language="javascript"> function imgGenerator(){ var question=confirm("Would you like to share an image?"); if (question==true){ var obj=document.getElementById("mngl-board-post-input"); var imgurl = prompt("To Share an image paste the image url here"); var txt=document.createTextNode("[img]"+imgurl+"[/img]"); obj.appendChild(txt); } else {} } </script> Hi there guys! My group is working on a basic document management system that includes four users. staff member, department director, division chief and clerk. The programming language that we used is PHP. when staff member logs-in on his homepage and uploads a file he must choose reviewer from a dropdown menu that consists of dep. director and div. chief. When he clicks the upload button the homepage of who he chose as reviewer should receive a prompt/alert box upon log-in saying "you have a document to review". For example: if he chose division chief as the reviewer when the person logs-in on his homepage a prompt/alert should display that he has something to review. The code for uploading file is already up and working. We are looking for advice on how to integrate javascript in this system. Especially in the prompt part. thanks for your ideas. I am trying to get these prompt boxes to display for age and resting heart rate. I cannot get this to work...This is what I have...any suggestions? <html> <head> <h1>Calculate Your Target Heart Rate</h1> <p>You can calculate your heart rate so that you can get the maximun results from your cardiovascular workout. Just follow these simple steps:</p> <script type="text/javascript"> function show_prompt() { var number = prompt ("Please enter your Resting heart rate:","Enter Rest Heart Rate Here!"); if (number! null && number! = "") var age = prompt ("Please enter your Age", "Enter Your Age Here!"); { document.write ("Your Resting Heart Rate is" + number); document.write ("Your Age is" + Age); } } </script> </head> <body> <input type ="button" onclick = "show_prompt ()" value = "Start Calculating Here!"/> </body> </html> Ok, this HAS to be some stupid typo I can't find somewhere or something but I don't see where. Basically the alertbox works just fine when only the show_alert() function and the button calling it are in the code. But neither work if only the alert() function and the button calling that are in the code, nor does it work by itself. However I see no differences in how they are coded unless I am missing something very very simple. Code: <html> <head> <script type="text/javascript"> function show_alert() { alert("I am an alert box!"); } function alert() { alert("again"); } </script> </head> <body> <input type="button" onClick="show_alert()" value="Show alert box" /> <input type="button" onClick="alert()" value="CLICK me" /> </body> </html> Working on a project and I am trying to get error messages to pop up when either a number wasn't entered or another error message if nothing was entered. otherwise if a number was probably entered it would perform the proper function, heres where I am at, does anyone know what is wrong with it? it wont calculate or even throw up a pop up box. Code: <script language="JavaScript" type="text/javascript"> function check_numbers(){ var sal = document.temp_form.num3.value; var error_message = ""; if (sal == "") { error_message += "You must enter a value gross annual salary \n"; } else if (isNaN(sal)) error_message += "Value entered is not a number, please try again. \n"; } if (error_message != "") { alert ("Please correct the following errors: \n_________________________________\n\n" + error_message); } else{ Calculate( parseInt(sal)) } } function calculate() { var sal = Number(document.getElementById('num3').value); var fName = document.getElementById('num1').value; var lname = document.getElementById('num2').value; alert('Hello '+ fName + ' '+ lname + ', you would pay $'+ sal * .2 + ' in Federal taxes and $'+ sal * .1 + ' in State taxes, leaving you $'+ sal * .7 + ' to take home annually!'); } And then here is the HTML part of it. Code: <b> Here is a pay slip generator that will determine your net annual salary. <br> Simply type in your first and last name along with gross salary and hit calculate. <br> Doing this will let you know how much you will end up paying in both Federal and State taxes. </b> <form action="" method="post" name="temp_form"> <p> Enter your First Name: <input name="num1" type="text" id="num1" size="10" maxlength="10"> </p> <p> Enter your Last Name: <input name="num2" type="text" id="num2" size="10" maxlength="10"> </p> <p> Enter your gross salary: <input name="num3" type="text" id="num3" size="10" maxlength="10"> </p> Click this button to calulate your annual net pay! <input type="submit" onclick="check_numbers()" value="Generate Pay Slip" /> <input type="button" value="Reset Form" onClick="this.form.reset()" /> </FORM> </body> </html> Using a variation of the Word-Launcher script found here (with my version below), I can type the word 'java' on a webpage the code is placed upon and have a nice alert box pop up for me. Whilst this works fine, I can't seem to figure out a way of enabling two variations of the same code on one page (for instance, have typing 'java' open up one alert box, and 'web' to open up another). I'm afraid my knowledge of JavaScript isn't terribly good, and would be most appreciative if someone could help me. Code: <SCRIPT LANGUAGE="JavaScript"> <!-- Begin var SpecialWord = "java", SpecialLetter = 0; function getKey(keyStroke) { var isNetscape=(document.layers); var eventChooser = (isNetscape) ? keyStroke.which : event.keyCode; var which = String.fromCharCode(eventChooser).toLowerCase(); if (which == SpecialWord.charAt(SpecialLetter)) { SpecialLetter++; if (SpecialLetter == SpecialWord.length) disp_alert(); } else SpecialLetter = 0; } document.onkeypress = getKey; // End --> </script> <script type="text/javascript"> function disp_alert() { alert("Hello again! This is how we" + '\n' + "add line breaks to an alert box!"); } </script> I have an user table like this:- guid | username | password | firstname | lastname | location | emailad dress | userrole -----------------------------------+----------+----------------------------------+-----------+-----------+----------+-------- ------+--------------- 8024259764dc3e8ee0fb6f5.84107784 | james | 827ccb0eea8a706c4c34a16891f84e7b | james | bond | NY | ny@live .com | administrator 18689183644dc3e91571a364.71859328 | saty | 250cf8b51c773f3f8dc8b4be867a9a02 | saty | john | NY | hk@fd.c om | administrator 2644885344cecd6f2973b35.63257615 | admin | 21232f297a57a5a743894a0e4a801fc3 | System | Generated | | | administrator (3 rows) now my postgre query for delete the row .... $query = "delete from users where username!= 'admin' and guid='".$guid."'"; $result = pg_query($conn, $query); ?> <script type="text/javascript"> alert("Cannot delete this .\n It is system generated(s)."); </script> <?php (1)when I delete the user name one by one then delete occurs in my page userlist.php, I donot want to delete admin so i use username!= 'admin' in where condition as shown above. (2)now when I del any username(3 rows) from user table then alert occurs & it delete from userlist.php after that my page userlist.php is blank. Finaly when i refresh the page then my admin username seen.. when i use return true; function then only alert generate .. delete doesnot occurs ... Actauly i want:- (1)if user is not admin then it delete from userlist.php ... nd also i m continue on this page ... like when james and saty want to delte their acount ..as given in table. (2)if user is admin then alert generate nd i m continue on this page. i m tired now plz help me .... so can anyone put the best condition in my coding. I've been asked to create a program which allows meteorologists to input a certain amount of data inputs of snowfall, and then the individual snowfall of each input itself. In my mind this should prompt me for a data input as many times as their are inputs, however, it works only for the first data input, then it returns to blank page without prompts for the second input... Any clues as to where I'm going wrong? Code: <script> var name = prompt('What\'s your name?'); function myOnLoad() { document.getElementsByTagName('name').innerHTML = name; } </script> <body onLoad="myOnLoad()"> This person's name is <name></name>.<br> <name></name> is someone who has just passed thorought this page.<br> Say bye to <name></name>! How come this code doesn't work? How do i use html forms in the same way as prompts? I want to do something like this: Code: <html> <head> <script type="text/javascript"> var cpu= prompt("What brand of CPU would you like?"); var ram= prompt("What brand of RAM would you like?"); var hdd= prompt("What brand of Hard Drive would you like?"); </script> </head> <body> <script type="text/javascript"> document.write("The brand of CPU I want is "+cpu); document.write("<br> The brand of RAM I want is "+ram); document.write("<br> The brand of Hard Drive I want is "+hdd); </script> </body> </html> But instead of having a notification box, I want a form. Is this possible? I dont like how it pops up... Trying to get this prompt window to work so I can begin a calulation function--Am I even in the ballpark? var chargeAmount = window.prompt("Enter a charge amount", Enter charge amount here."); var discountPercent; var totalCharges; function confirmSubmit() { if (document.forms[0].name.value == "" || document.forms[0].name.value == "Enter your name") { window.alert("You must enter your name."); return false; } function confirmReset() { var resetForm = window.confirm("Are you sure you want to reset the form?"); if (resetForm == true) return true; return false; } } /* ]]> */ </script> </head> <body> <h2>Charge Information</h2> <form action="FormProcessor.html" method="get" enctype="application/x-www-form-urlencoded" onsubmit="return confirmSubmit();" onreset="return confirmReset();"> <p>Customer Name: <input type="text" name="name" value="Enter your name" /> </p> <p>Customer Type:</p> <input type="radio" name="rb" value="regular" />Regular<br /> <input type="radio" name="rb" value="silver" />Silver<br /> <input type="radio" name="rb" value="gold" />Gold<br /> <br /><br /> Total Charges:  <input type=text value="" name="total charges" size=10;> <br /><br /> Discount:  <input type=text value="" name="discount" size=10;> <br /><br /> Payment:  <input type=text value="" name="payment" size=10;> <input type="button" name="calc" value="Calculate Payment" onClick="return calcpay(this.form)"> <input type="reset" name="reset" value="Reset Form" > </form> </body> We were given a task at school today. Basically we need to have a prompt box popup, and the user enters numbers 1-12 (1 for each month of the year). The prompt keeps popping up until the user types "stop". It will display the months that were entered. Could anyone assist me with this, thanks. I'm working on an html form that will be launched from within another application, but every time it launches the form none of the JS coding works because of the stupid IE security. If I launch the form from outside the application I just have to select "allow blocked content" from that stupid information bar that says "to help protect your security IE has restricted the webpage from running scripts...." I added the application site to our trusted sites and basically turned off security for that zone but it still doesn't work. Obviously there is a way to run JS without allowing the content, but I don't do enough coding to know how that is done. Can someone help me out please? I need the JS to run automatically without that information bar appearing at all. After searching this site I could not find any old posts on how to position the PROMPT box. As default (as you all most likely know) the prompt box in IE browser comes up in the top left corner. not very visable and would like to center it so it would be more visiable to the user being prompted. Can someone help me with this? thanks John I am new to learning JS and am trying to create an array through a prompt. It seems to work, but I believe it is treating the prompted numbers as strings not numbers. I am parsing the negatives and zeros, and positives and counting them. It doesn't recognize the negative sign. Here's my js: function counter() { var numArr = new Array(Number(prompt("Please enter and array of numbers, in any order, separated by a comma..." + '\n' + "In the following format: -2,0,2"))); var positives = 0; var negatives = 0; var zeros = 0; for (var i = 0; i < numArr.length; i++) { switch (true) { case numArr[i] < 0: negatives++; break; case numArr[i] == 0: zeros++; break; case numArr[i] > 0: positives++; break; } } alert("You entered:"+ '\n' + "Number of Negatives: " + negatives + '\n' + "Number of Zeros: " + zeros + '\n' + "Number of Positives: " + positives); } counter(); Any suggestions? Hello All, I have made a prompt and marked it in bold below. I would like text to appear after what the person has entered At current it reads Hello (Name Prompt) I would like it to appear saying something on the lines of Hello (Name Prompt) Enjoy The Game. Any Ideas? <html> <body> <SCRIPT LANGUAGE="JavaScript"> <head> <TITLE>Find the Correct Number Game</TITLE> </head> a=(prompt('please enter your name:',0)); // prompt for player name document.write(' Hello ' ,a); var guessme=Math.round(Math.random()*(49)+1); var speech='Choose a number between 1 and 50'; function process(hiddennumber) { var guessnumber=document.forms.guesstable.guessnumber.value; var speech='"'+guessnumber+ '" Letters are not allowed. Please enter digits only!.'; document.forms.guesstable.guessnumber.value=''; if (guessnumber==hiddennumber) { document.forms.guesstable.prompt.value='Congratulations! '+hiddennumber+' is correct!'; alert ('Well done - the mystery number is '+hiddennumber+'! \n\nIf you want to play again click the button.'); speech=''; document.location=document.location; } if (hiddennumber<guessnumber) { speech='Less than '+ guessnumber; } if (hiddennumber>guessnumber) { speech='Greater than '+ guessnumber; } if (guessnumber=='') { speech='You didn\'t guess anything!' } document.forms.guesstable.prompt.value=speech; document.forms.guesstable.guessnumber.focus(); } </SCRIPT> </body> </html> <FORM onSubmit="" NAME="guesstable"> <CENTER> <TABLE ALIGN="left" BGCOLOR="#888889" BORDER="5" CELLPADDING="5"> <TR> <TD BGCOLOR="#004081"> <FONT COLOR="#ffffff" FACE="Arial"><B>Find the Correct Number</B></FONT> </TD> </TR> <TR> <TD> <CENTER> <INPUT TYPE="text" NAME="prompt" SIZE="31" MAXLENGTH="60" VALUE="Find the correct number which is between 1 and 50"><BR> <INPUT TYPE="text" NAME="guessnumber" SIZE="3" MAXLENGTH="3" VALUE=""> <INPUT TYPE="button" VALUE="Enter" onClick='process(guessme)'> </CENTER> </TD> </TR> </TABLE> </CENTER> </FORM> I am ignorant in java scripting. I want to use the following java script to create the radiobutton in obiee reporting. This java script will change the regular prompt to radio button,however I do not know how to debug if it is working. Waiting for kind reply. Anil PHP Code: <script type="text/javascript"> function getElementsByClass(searchClass,node,tag) { var classElements = new Array(); if ( node == null ) node = document; if ( tag == null ) tag = '*'; var els = node.getElementsByTagName(tag); var elsLen = els.length; var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)"); for (i = 0, j = 0; i < elsLen; i++) { if ( pattern.test(els[i].className) ) { classElements[j] = els[i]; j++; } } return classElements; } var tables = getElementsByClass('DashboardPromptViewTable',null, 'table'); for (var table = 0; table < tables.length; table++){ var stringFunc = ''; var selects = tables[table].getElementsByTagName('select'); if ( selects.length == undefined || selects.length == 0 || selects[0].name === 'Match'){ if (debug === 1) { document.write("No selects found. Continue to next record."+"<BR>");} continue; // This DB Prompt doesn't have any select statements } var spans = getElementsByClass('minibuttonOn',tables[table],'span'); spans[0].style.display="none"; stringFunc = String(spans[0].getElementsByTagName('a')[0].onclick); stringFunc = stringFunc.substr(stringFunc.indexOf('{')+1, stringFunc.length - 2 - stringFunc.indexOf('{')); for (var s =0; s < selects.length; s++){ var new_form = document.createElement('form'); var options = selects[s].getElementsByTagName('option'); new_form.name = selects[s].name+"_radio"; new_form.id = selects[s].id+"_radio"; for (var o=0; o<options.length; o++) { var new_input = document.createElement('input'); new_input.type = 'radio'; new_input.value = options[o].value; new_input.name = selects[s].name+"_radio"; if(selects[s].selectedIndex == o){ new_input.checked = true; } var onClickStringFunc = "var sel = document.getElementById(this.name.substr(0,this.name.length-6));" + "if ( sel == null){"+ "return;"+ "}"+ "var opts = sel.getElementsByTagName('option');"+ "for( var opt = 0; opt < opts.length; opt++){"+ "if( opts[opt].value == this.value){"+ "sel.selectedIndex = opt;"+ "}"+ "}"; new_input.onclick = new Function(onClickStringFunc + stringFunc); var new_text = document.createTextNode(options[o].innerHTML); new_form.appendChild(new_input); new_form.appendChild(new_text); } // end options for loop selects[s].parentNode.insertBefore(new_form,selects[s]); selects[s].style.display="none"; } // end selects for loop } // end table foor loop </script> I am fairly new to Javascript. From what I can compare to sample codes this should work. I have a button with an onclick that opens a prompt. Compares whats written to a php SESSION variable. If true redirects, if false displays an alert. However, all of this means nothing because when I press the button, no prompt comes up! Heres the code Code: <head> <script type="text/javascript"> function passPrompt() { var pass=prompt("Please enter your password:","") if pass = <?php $pwd ?> header('Location: setup.php'); else alert("Password is incorrect"); } </script> </head> ... <input id="settings" type="button" name="settings" value="Settings" onclick="passPrompt()" /> Issue: I have a java script that bring up a simple password box, It works great in Chrome, Safari, firefox but I get a box in Internet explorer asking my customers to take another step to allow access. This is not good, Customers did not now what to do. Error: This website is using a scripted window to ask you for information. If you trust this website, click here to allow scripted windows Microsoft response: "Internet Explorer has blocked a website from using a small program (called a script) to display a separate window. Hackers sometimes use scripted windows to mimic legitimate windows, such as login screens, that appear on websites. If you trust the website and want to allow the scripted window, click the Information Bar, and then click Temporarily Allow Scripted Windows. To always allow scripted windows, check the Allow websites to prompt for information using scripted windows custom security setting." Objective: I now there is likely no way to by pass this, so i need a different solution. Possible a Form Box to be present on the site that when information is entered it passes the password to the javascript code to allow access? Please help, this is actually urgent as I have already lost a customer :-( Code: : <script> function getStyle() { var temp = document.getElementById("admintable").style.visibility; return temp; } function switchMain() { var isAuthed = authenticate(); if(isAuthed){ var current = getStyle(); if( current == "visible" ){ document.getElementById("admintable").style.visibility = "hidden"; }else{ document.getElementById("admintable").style.visibility = "visible"; } } } function authenticate() { if(readCookie('authenticated')!=null) { return true; } else{ var password=prompt("Enter Code","") if (password =="1234") { createCookie('authenticated',true); return true; } } return false; } function readCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i < ca.length;i++) { var c = ca[i]; while (c.charAt(0)==' ') c = c.substring(1,c.length); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGMTString(); } else var expires = ""; document.cookie = name+"="+value+expires+"; path=/"; } </script> Here is what I found online: http://www.anyexample.com/webdev/jav...lternative.xml UPDATE: I am a n00b, I need to incorporate this code below to the one above. Thanx Philip Code: <input type = "password" name = "pwd" id = "pwd" onchange = checkPassword(this.value)" <script type = "text/javascript"> function checkPassword(which) { if (which != "1234") { // password is visible!!! alert ("Incorrect password - please try again"); document.getElementById("pwd").value =""; return false; } else { alert ("Password is correct!"); //do more stuff here, e.g create the cookie } } </script> I'm slowly learning javascript, have a little java background. I wanted to just make a button you push to enter a prompt. The problem was I want the prompt message to print in a new window. Here is what I have. Can anyone help me with this? <html> <head> <script type="text/javascript"> function show_prompt() function openindex() { OpenWindow=window.open("", "newwin", "height=250, width=250,toolbar=no,scrollbars="+scroll+",menubar=no"); { var name=prompt("Please enter your name",""); if (name!=null && name!="") { OpenWindow.document.write("Hello " + name + "! How are you today? Welcome to my Demo page, Enjoy!"); } } } </script> </head> <body> <input type="button" onclick="show_prompt()" value="Click Me " /> </body> </html> |