JavaScript - Simple Validation Not Working After Alert
I have used this formula before, for some reason it isnt working properly. If the value is equal to 0 it will give an alert and return false. Then if you change the value of the drop down it won't let you submit.
Does anyone see anything wrong? Code: <script type="text/javascript"> <!-- function validate_form ( ) { valid = true; if ( document.week_picks.blowout.value == "0" ) { alert ( "Please Select a Blowout" ); } return false; } //--> </script> Code: <form name="week_picks" method="post" action="confirm.php" onsubmit="return validate_form()"> <select id="blowout" name="blowout"> <option value="0">..</option> .... </select> Thanks for any help. Similar TutorialsHello, I'll be honest I dont know much about javascript so hopefully one of you lovely people can help. My issue is I would like to have this code validate my radio buttons on my form and also have an onscreen pop-up in the case that the user is exiting the page without filling out the form. I have had both of these peices of code working...but when I put both of them on the page the pop-up will override the validation script, and I don't wish to have the pop-up, pop-up when the user hits the submit button. Here are my pieces of code: Inside my head tag: Code: <script language="JavaScript"> function closeIt() { return "\n********************************************\nWAIT! WAIT! WAIT! WAIT! WAIT! WAIT!\n********************************************\n\nDon't leave just yet...\n\nDon't worry - We will never rent, sell or\nshare your contact information, with\nanyone else. We respect your privacy\nand hate spam too!\n\n"; } window.onbeforeunload = closeIt; </script> <script language="JavaScript"> function valbutton(thisform) { // place any other field validations that you require here // validate myradiobuttons myOption = -1; for (i=thisform.infusion_option_0.length-1; i > -1; i--) { if (thisform.infusion_option_0[i].checked) { myOption = i; i = -1; } } if (myOption == -1) { alert("You must select an event city date"); return false; } // place any other field validations that you require here thisform.submit(); // this line submits the form after validation } </script> And then for my form and submit code I have: Code: <form name="myform" action="https://mydomain.com/AddForms/processFormSecure.jsp" method='POST' onSubmit="return NoneWithCheck();"> <input id="Submit" name="Submit" type="submit" value="Submit" onClick="valbutton(myform);return false;" /></form> Any help would be appreciated, thanks. Hi everyone. New programmer here trying to get fix this one specific issue I am having and I could use some help. Using Javascript, ASP.net with C# code behind. I am validating a textbox using the onblur event. It is being validated in that it must have an alphanumeric entry before the user tabs off of it to the next box. The validation part works, the part that doesn't work is that I get the validation alert textbox if I close the window. I have a workaround in that I am assigning that textbox a character using windo.onberforeunload but I know that isn't right. Here is what I have so far... Code behind on Page_Load... Code: txtCLBRTNWC.Attributes.Add("onblur", "return reqVLD(this)"); here is my textbox field... Code: <asp:TextBox ID="txtCLBRTNWC" runat="server" Width="75" MaxLength="4" ontextchanged="txtCLBRTNWC_TextChanged" TabIndex="1" onkeyup="clbwclngCHK(this);" ></asp:TextBox> And here are the 2 functions being used... Code: function reqVLD(alphanumericChar) { var chk = /^[a-zA-Z0-9]+$/; // var boxid = document.getElementById("<%=txtCLBRTNWC.ClientID%>").value; // var matchArray = boxid.match(chk); if(document.getElementById("<%=txtCLBRTNWC.ClientID%>").value.match(chk)) { return true; } else { document.getElementById("<%=txtCLBRTNWC.ClientID%>").focus(); document.getElementById("<%=txtCLBRTNWC.ClientID%>").value = ""; alert("Please enter a Work Center."); return false; } } function clbwclngCHK(obj_in)//When txtCLBRTNWC entry hits the max of 4 auto focuses to next box. { if (obj_in.value.length == 4) document.getElementById("<%=txtSTRTDT.ClientID%>").focus(); } Here is my work around... Code: window.onbeforeunload = function () { document.getElementById("<%=txtCLBRTNWC.ClientID%>").value = "1"; } Summary. User has to enter something in box before they can move on. This is checked and validating using an onblur event. Problem is, the onblur event fires if closing\exiting the window. I know there has to be a better way of doing this. Any advise or help would be greatly appreciated. Thanks Will Hi I have a problem with a form in my site he http://www.21centuryanswers.com/submit.php if no field is filled and you click submit, an alert will be shown, yet the next page will still load! How do I fix it? the code for the form is: <form action="privacy.php" method="post" onsubmit="return checkform(this);"> <fieldset> <center> E-mail: <input type="textfield" name="email" size="60" value="" /><br/></br> Question: <input type="textfield" name="question" size="70" value="" /><br/><br/> <input type="submit" value = "Submit"/> </center> </fieldset> </form> and here is the validation script: <script language="JavaScript" type="text/javascript"> <!-- function checkform ( form ) { // ** START ** if (form.email.value == "") { alert( "Please enter your email." ); form.author.focus(); return false ; } if (form.question.value == "") { alert( "Please enter the question." ); form.title.focus(); return false ; } // ** END ** return true ; } //--> </script> Please help! Hi all, new here. Hope this makes sense. In Salesforce, I am adding what they call an "S-Control" via HTML/JavaScript that will display an alert if certain field criteria are met. The alert would be a reminder to hit the "Submit for Approval" button if the Quote Estimate is equal to or greater than $50,000. For testing purposes I added another criteria, that the Opportunity name must = Test and Stage must = Proposal/Price Quote. Here's what I've come up with so far, taking from other examples, but I receive no alert, so I'm wondering where it went wrong. Code: <html> <head> <script type="text/javascript" language="javascript" src="/js/functions.js"></script> <script type="text/javascript" src="/soap/ajax/10.0/connection.js"></script> <script type="text/javascript"> function throwalert() { // Begin by creating 3 variables for the criteria to be met. var oppname = "{!Opportunity.Name}"; var isstatus = "{!Opportunity.StageName}"; var quoteest = "{!Opportunity.Quote_Estimate__c}" // Now create a function which will decide if the criteria is met or not and throw alert message. //var oppname= "Test" //var quoteest >= "50000" //var isstatus = "Proposal/Price Quote" var msgg = "The quote estimate for this opportunity is equal to or greater than $50,000. Remember to submit this opportunity for approval. " if ((oppname == "Test") && (quoteest >= 50000) && (isstatus == "Proposal/Price Quote")) { alert(msgg); } else { window.parent.location.replace = "{URLFOR($Action.Opportunity.View, Opportunity.Id,null,true)};" } } </script> </head> <body onload="throwalert()";> </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> hye all, it's me again. i have a little bit confusion here. why is this alert "alert("Please answer the question.");" did not prompt out even if i don't select any of the radio button?can anyone help me? i appreciate if someone can help me.TQ. Here is the coding to return the radio buttons' value: Code: function getSelectedValue(flds) { var i = 0; var len = flds.length; while (i < len) { if (flds[i].checked) { return flds[i].value; } i++; } return ""; } Below is the coding to calculate the radio button value: Code: var bdpr=0,bdft=0,bdbs=0,prft=0,prbs=0,ftbs=0; var rank= new Array(); var form = document.forms.Calculator; var bdpr = Number( getSelectedValue(form.elements.a) ); var bdft = Number( getSelectedValue(form.elements.b) ); var bdbs = Number( getSelectedValue(form.elements.c) ); var prft = Number( getSelectedValue(form.elements.d) ); var prbs = Number( getSelectedValue(form.elements.e) ); var ftbs = Number( getSelectedValue(form.elements.f) ); if ( isNaN(bdpr)|| isNaN(bdft) || isNaN(bdbs) || isNaN(prft) || isNaN(prbs) || isNaN(ftbs)){ alert("Please answer the question."); } else{ brand=1+bdpr+bdft+bdbs; price=1+1/bdpr+prft+prbs; feature=1+1/bdft+1/prft+ftbs; basic=1+1/bdbs+1/prbs+1/ftbs; window.open("try.html"); } I am trying to get the total of the checkboxes selected when the user clicks the "calculate total" button. It isn't working though. What am I doing wrong? Code: <html> <head> <script type="text/javascript"> function calculate(f) { var nums = f.num; var ntext = f.numtext; var nitem = f.numitem; var result = 0; var items = ''; for(var i=0;i<nums.length;i++) { if(nums[i].checked) { result+=parseFloat(ntext[i].value); items+=nitem[i].value+'\n'; } } f.answer.value=result; } if (nums[i].checked) { window.alert("Your total is " + f.answer.value=result;); } </script> </head> <body> <style type="text/css"> .box1 { float:left; width:500px; clear:left; } .box2 { float:left; padding-left:50px; } </style> <form name="myform"> Build your own family meal<br> <div class="box1"> <input type="checkbox" name="num" onclick="calculate(this.form)"> <input type="hidden" name="numtext" value="24.00" onchange="calculate(this.form)"> <input type="hidden" name="numitem" value="pencil" onchange="calculate(this.form)"> <div class="box2">Chicken Enchiladas $24.00</div> </div><br> <div class="box1"> <input type="checkbox" name="num" onclick="calculate(this.form)"> <input type="hidden" name="numtext" value="15.00" onchange="calculate(this.form)"> <input type="hidden" name="numitem" value="pen" onchange="calculate(this.form)"> <div class="box2">Rice and Beans $15.00</div> </div><br> <div class="box1"> <input type="checkbox" name="num" onclick="calculate(this.form)"> <input type="hidden" name="numtext" value="6.00" onchange="calculate(this.form)"> <input type="hidden" name="numitem" value="paper" onchange="calculate(this.form)"> <div class="box2">Chips and Salsa $6.00</div> </div><br> <div class="box1"> <input type="submit" name="answer" value="Calculate Total" onclick="calculate(f)" /> </div> </form> </body> </html> I have attempted several variations of this, none of which seem to help. I have narrowed this down to being the central problem at hand. Do i just have something out of place? Code: if (monthly_pay>=1000) {bank_account="Okay";} else if ((monthly_pay>=700)&&(monthly_pay<=999)) {bank_account="Still Okay";} else if ((monthly_pay>=500)&&(monthly_pay<=699)) {bank_account="Start to worry";} else if ((monthly_pay>=300)&&(monthly_pay<=499)) {bank_account="Panic";} else if (monthly_pay<=299) {bank_account="Chapter 9";} else {document.write("Marry Up")} } Optionally, could I not do else if and somehow use this as a Nested If? Any help appreciated, Thank You 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. Hi folks this is my first post Edit: sorry for the incorrect title should have read simple javascript validation problem! i have a fair understanding of php and mysql and have never had to use javascript as i was secretly trying to avoid it. The time has now come where i want to use it for client side validation on a form. I am able to validate most of fields however i need to validate a date to check it has been entered in the following format dd/mm/yyyy and that the date is not greater than todays date. N.B the date is entered into a text field in HTML i wrote a function with lots of if/else statements and failed. Im 100% positive there is a far simpler way to achieve my goal any help would be greatly appreciated. Thanks Code: function checkDate(elem, helperMsg){ var day = elem.substr(0,2); var month = elem.substr(3,2); var year = elem.substr(6,4); if(day < 01 || day > 31) { var invalidday = true; } if (month < 0 || month > 12) { var invalidmonth = true; } if (year < 01 || year > 2099) { var invalidyear = true; } if ( month==04 || month==06 || month==09|| month==11) { if (day == 31) { var invalidday = true; } } if (month==02) { if (day > 28) { var invalidday = true; } } if (invalidday || invalidmonth || invalidyear) { alert(helperMsg); elem.focus(); return false; } else { var dd = today.getDate(); var mm = today.getMonth()+1;//January is 0! var yyyy = today.getFullYear(); if(dd < 10) { dd='0'+dd; } if(mm < 10) { mm='0'+mm; } } if (day > dd && month > mm && year > yyyy) { alert(helperMsg); elem.focus(); return false; } else { return true; } } I am trying to have the page go to another page after clicking the submit button. is it possible to add to this function an else statement ? if form is filled then go to this page Code: function notEmpty(elem, helperMsg){ if(elem.value.length == 0){ alert(helperMsg); elem.focus(); return false; } return true; } Hello all, I have had a look around the forums, but cannot find the answer that I am after. basically, I have a text area to enter a search, and I have already written the JavaScript part of it (unsure if this is where the problem lies). The text that will go into the text area is a URL. the search button is disabled if there are spaces in the query, or if the field is blank, else the button is enabled. The problem I'm having may or may not be to do with my script. The HTML is set to 'onKeyUp' and 'onBlur' to run the function. now, it works fine, unless the user uses the mouse to copy and paste a URL into the text area. the button remains disabled until the user focuses out of the text area then back in. if ctrl+v is used, then the function works. If you would like to see the script I have written let me know, I hope you have some ideas Thanks in advance! Hey all, I'm finishing up a project at work that has a web-based front end. Our tightly compressed schedule has left me with no time to properly learn Javascript, so I need some help. I have two pages that need client side validation... Page 1: several text inputs, one select, one textarea, and (maybe) a checkbox There are some complex things I could do here, but for now, I just need to make users behave. The textarea must have something in it, certain select options need a Yes/No popup, and if the checkbox is written to the page (ASP determines this) then it must be checked before certain select options are submitted. I could copy some examples I've found, but how do I ignore the checkbox when it doesn't exist? Page 2: two selects and a textarea Options must be selected and the textarea must have something in it. Straightforward, except that it's a dynamic form where each element has a name + i where i is a number that increments each time the user expands the form. Will wildcards work here? I know this looks like "do my homework" but any help would be greatly appreciated. This is desired to work in IE6, but if that can't happen, I can force an IE8 upgrade on everyone. Code: if(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailvalue)) this is a code to validate an email address. My question is.. how do you call this type of validation? Those /^\w+([\.-]?\w+.. i don't really understand how they work. Is there a special term used to describe this? I want to learn how it works. I want to search for tutorials online but i cant think of any desriptive word to use as keyword for google search. I cant find helpful results. Please help. Thanks I have a simple validation script. The first line of the validation is: function Validate() { if (document.form.name.value == '') { the id of my form is form, so my query is will this validation work, as it does not have a name but it has an id. Should I change the name (above) to id? Would appreciate some help making some minor enhancements to a pet adoption form: http://pawsct.org/application.php Problem - Applicants don't read. They answer "No" to the "Agree to Shelter Visit" question, then fill out the looooong application, and get abusive when told that they're not eligible for adoption. They apply from Florida and ignore the warnings about the 90 mile limit. Text notes just don't make enough of an impact. I want to use OnChange to give an alert if someone does not answer "Yes" to agree to a shelter visit (i.e. popup "You must agree to a shelter visit to qualify for an adoption from PAWS") and perhaps a warning message (red text reminder about the 90 mile limit next to the drop-down box) if someone enters a state other than CT. Second problem - I'm a volunteer who doesn't know javascript and can't cobble together a SIMPLE, workable solution from other code samples. Any assistance that you can provide would be most welcome and appreciated. Note: we are planning a redesign of the web site and may do some more sophisticated form validation (e.g. check age, valid e-mail, valid phone) in the future. But for now, we're just looking to check a few key fields for answers that would immediately disqualify an applicant. I'm trying to do a simple validation to see if the field is blank, if it is, I want to write a message to a div. I've added the function call to an onclick. It seems to be set off no matter if there is a value or not, and what's more, the fields are clearing out when the link is clicked. My brain is mush right now, so what am I missing? Code: function validateThis() { document.getElementById('messages').innerHTML = '' var theMessage = ''; var reqMsg = 'must have a value.'; for (var i = 0; i <document.theForm1.elements.length; i++) { if (document.theForm1.elements[i].value = ' ') { theMessage += document.theForm1.elements[i].name +' ' + reqMsg +'<br/>'; }} document.getElementById('messages').innerHTML = theMessage; } I am a javascript begginer and would like to know what is this bit of code saying? (email.indexOf("@",atPos+1) > -1) Thank you! Code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Email checker</title> <script type="text/javascript"> function validEmail(email) { invalidChars = " /:,;" if (email == "") { return false; } for (i=0; i<invalidChars.length; i++) { badChar = invalidChars.charAt(i) if (email.indexOf(badChar,0) > -1) { return false; } } atPos = email.indexOf("@",1) if (atPos == -1) { return false; } if (email.indexOf("@",atPos+1) > -1) { return false } periodPos = email.indexOf(".",atPos) if (periodPos == -1) { return false; } return true; } function submitIt(emailForm) { if (!validEmail(emailForm.emailAddr.value)) { alert("Invalid email address"); emailForm.emailAddr.focus(); emailForm.emailAddr.select(); return false } alert("email is correct!"); return true; } </script> </head> <body> <h2>Email checker</h2> <form onsubmit="return submitIt(this)" action="#"> Email Address: <input name="emailAddr" type="text" size="30" /> <p><input type="submit" value="Submit" /> <input type="reset" /></p> </form> </body> </html> |