JavaScript - Php Array[] Javascript Validation Help Needed
Hi,
I have a .php page that have a multiple check box question that requires validation whether or not the user checked any values. ----------------------------- HTML: ----------------------------- <p><font color="#FF0000">*</font> If you are a health professional, what is your practice setting (check all that apply):<br> <input name="Q2_PracticeSetting[]" type="checkbox" value="Hospital"> Hospital<br> <input name="Q2_PracticeSetting[]" type="checkbox" value="Outpatient setting"> Outpatient setting<br> <input name="Q2_PracticeSetting[]" type="checkbox" value="Academia"> Academia<br> <input name="Q2_PracticeSetting[]" type="checkbox" value="Research"> Research<br> <input name="Q2_PracticeSetting[]" type="checkbox" value="Other"> Other </p> ----------------------------- Using Javascript: ----------------------------- function ValidateForm() { field = document.register.firstname; if (isBlank(field, "First Name")) return false; field = document.register.lastname; if (isBlank(field, "Last Name")) return false; field = document.register.credential; if (isBlank(field, "Credentials")) return false; field = document.register.email; if (isBlank(field, "Email Address")) return false; if (!isEmail(field, "Email Address")) return false; var chks = document.register.elements['Q2_PracticeSetting[]']; var hasChecked = true; for (var i=0;i<chks.length;i++){ if (chks[i].checked){ hasChecked = false; break; } } if (!hasChecked){ alert("Please select at least one."); chks[0].focus(); return false; } return true; } ----------------------------- Result: ----------------------------- Not working Any help is much apprciated! Similar TutorialsBelow is some code I wrote for my assignment that is due tonight; however, I can't seem to get any of the validation features to work. Can someone help me? I'm really new to javascript and struggling through it, but really want to figure this out. Here are the things I'm trying to do: -Validate each textbox to make sure they aren't empty -Validate one textbox to make sure that the text entered is numeric only -Validate the two email addresses for proper email format -Validate the two email addresses to make sure they are exactly alike -Show all of your errors at once, in one alert box, not individually. -Only check for matching emails if the first is valid. Here is my attempt at the code: [CODE] <script type="text/javascript"> <!-- Hide from older browsers var ProductInquiryForm; function Validate( ProductInquiryForm ) { formName = ProductInquiryForm; } function ValidEmail( EmailSearch ) { var txtEmail = EmailSearch.value; var intAtSign = txtEmail.indexOf("@"); var intLastDot = txtEmail.lastIndexOf("."); if( txtEmail == "" || txtEmail == null ) { return false; } if( intAtSign == -1 || intLastDot == -1 ) { return false; } if( intLastDot < intAtSign ) { return false; } if( intLastDot - intAtSign == 1 ) { return false; } if( intLastDot >= txtEmail.length-2 ) { return false; } else { return true; } } function ValidEmail( ConfirmEmailSearch ) { var txtEmail = ConfirmEmailSearch.value; var intAtSign = txtEmail.indexOf("@"); var intLastDot = txtEmail.lastIndexOf("."); if( txtEmail == "" || txtEmail == null ) { return false; } if( intAtSign == -1 || intLastDot == -1 ) { return false; } if( intLastDot < intAtSign ) { return false; } if( intLastDot - intAtSign == 1 ) { return false; } if( intLastDot >= txtEmail.length-2 ) { return false; } if (EmailSearch!= ConfirmEmailSearch) { return false; } else { return true; } } function ApprovedEmail ( ConfirmEmailSearch ) { if( HasText(EmailSearch) && ValidEmail(EmailSearch) && HasText(ConfirmEmailSearch) && ValidEmail(ConfirmEmailSearch)) { return true; } else { alert("A valid email must be entered and match in both fields!"); return false; } } function validateZIP(ZIPSearch) { if( ZIPSearch.value != 0||1||2||3||4||5||6||7||8||9) { alert("Please enter digits only for the ZIP code."); return false; } if (field.length!=5) { alert("Please enter no more than 5 digits for your ZIP code."); return false; } function HasText( ProductSearchDescription ) { if( ProductSearchDescription.value.length != 0 ) { return true; } else { alert("Please enter your product needs."); return false; } } // Stop hiding --> </script> <noscript> This site uses JavaScript code for validation and calculations based on user input. </noscript> <form name="form1" id="form1" method="post" action="intercept-searchform.asp" onsubmit="return Validate(this)"> <div> <label for="EmailSearch">Email Address:</label> <input name="EmailSearch" type="text" class="TextBox" id="EmailSearch" /> </div> <div> <label for="ConfirmEmailSearch">Confirm Email Address:</label> <input name="ConfirmEmailSearch" type="text" class="TextBox" id="ConfirmEmailSearch" /> </div> <div> <label for="ZIPSearch">ZIP Code</label> <input name="ZIPSearch" type="zip" class="zip" id="zip" /> </div> <div> <label for="ProductSearchDescription">Describe the product you are looking for.</label> <textarea name="ProductSearchDescription" id="ProductSearchDescription" class= "Comments" rows="6" cols="50"></textarea> </div> <br /> <div id="buttons"> <input name="Submit" type="Submit" value="Submit" /> <input name="Reset" type="Reset" /> </div> </form> [CODE] Thank you so much to anyone willing to help give me some guidance! Hello, I have just started learning JavaScript, so I do not know much about it at this moment. I need some help regarding validation of input values which are actually in an array. Okay, my HTML looks like this: Code: Link# 1: <input name="url[]" size="80" type="text"> Title# 1:<input name="title[]" size="80" type="text"> Link# 2: <input name="url[]" size="80" type="text"> Title# 2:<input name="title[]" size="80" type="text"> Link# 3: <input name="url[]" size="80" type="text"> Title# 3:<input name="title[]" size="80" type="text"> . . . . . And so on. (up to 20) User can add more input fields by adding "Add" button; I'm using JavaScript for that purpose. By default, only a couple of fields is shown. I want to validate these all fields using a loop or a number of loops, such that an alert appears if any field is left blank and also if any value in title[] array matches another value in that array and same for the second array url[] Examples of working would be something like this: Code: If Link# 1 is left blank: alert("Link# 1 is empty"); or If Link# 2 is left blank: alert("Link# 2 is empty"); or If Title# 1 is left blank: alert("Title# 1 is empty"); or If Title# 2 is left blank: alert("Title# 2 is empty"); or If Link# 1 == Link# 2: alert("Link# 1 is same as Link#2"); or If Link# 1 == Link# 3: alert("Link# 1 is same as Link#2"); or If Title# 1 == Title# 3: alert("Title# 1 is same as Title#3"); etc, etc. Any help would be appreciated. Thank you. Hi, My code seems to work individually it is when I try to do multiple input validation that everything goes wrong. I have tried at the moment to cut it down to just two validation inputs to simplify things, but the more I try and play around with it the worse it seems to get. I know a lot of people post about this but I have tried comparing to other people's codes and solutions and just can't work out what is wrong. I would be incredibly grateful for any help. Code: <html> <title>Sign Up</title> <head> <script type="text/javascript"> function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@"); dotpos=value.lastIndexOf("."); if (apos<1||dotpos-apos<2) {alert(alerttxt);return false;} else {return true;} } } function validate_fname(field,alerttxt) { with (field) { if (fname=="enter firstname" OR fname="") {alert(alerttxt);return false;} else {return true;} } } function validate_form(thisform) { with (thisform) { if (validate_email(email,"The email address you entered is not a valid email address!")==false) {email.focus();return false;} else {return true;} } { if (validate_fname(fname,"Please enter your firstname!")==false) {cemail.focus();return false;} else {return true;} } } </script> </head> <body> <form action="userdetails" id="signupForm" onsubmit="return validate_form(this)" method="post"> <fieldset class="two"> <legend>Your Information:</legend> <br /> <br /> <label class="two">First name:</label> <input type="text" class="input" required="required" name="fname" value="enter firstname" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'enter firstname':this.value;" size="30%" /> <br /> <br /> <label class="two">Email:</label> <input type="text" class="input" name="email" value="enter email address" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'enter email address':this.value;" size="30%" /> <br /> <br /> </fieldset> <br /> <br /> <input type="submit" value="Submit"/> </form> </body> </html> here is the html code that i have PHP Code: <td valign="middle" valign="middle"> <input type="radio" name="gender" id="genderM" value="Male" /> Male <input type="radio" name="gender" id="genderFM" value="Female" /> Female </td> and here is the js funtion PHP Code: var $j = jQuery.noConflict(); function isValidEmail(str) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); } function validateForm(){ var firstName; var lastName; var email; var mobile; var comment; var error; firstName = $j('#firstName').val(); lastName = $j('#lastName').val(); email = $j('#email').val(); mobile = $j('#mobile').val(); comment = $j('#comment').val(); if(firstName=='' || firstName.length < 3){ error = 'Please Enter Your First Name'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } if(lastName=='' || lastName.length < 3){ error = 'Please Enter Your Second Name'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } //mob //$jmob_pattern = '^\d{10}$j'; if(mobile.length != 10 || isNaN(mobile)){ error = 'Please Enter Your Mobile Number'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } if(email=='' || !isValidEmail(email)){ error = 'Please Enter Your Email Address'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } if(comment.length < 5){ error = 'Please Enter A Comment'; $j('#errormsg').html('<p class="errors">'+ error +'</p>'); return false; } return true; } Does anybody know how i check to see if the radio button is select and also can anybody tell me how i can check for an email in the correct format the function isValidEmail in the above alows emails to pass through if they are in this format aaa@aaa. i only want them to go through if they are aaa@aaa.com Thanks for your help if you give it Hi all, I am trying to build a config object for a program using a loop. Here's what I've done so far (note: "bbtags" is an array built like this): var bbtags = new Array((b),(/b),(i),(/i),(u),(/u),etc......); (note I used parentheses in place of brackets soas not to confuse the board.) Code: var arr = []; var len = bbtags.length; var tag = ''; var obj = ''; for (var i = 0; i < len; i+=2) { tag = bbtags[i].replace(/\[(.*?)\]/gi,'$1'); // strip off the brackets name = String('mce_' + tag); // generate a name obj = { name : { 'block' : name } } arr.push(obj); } The RESULT that I am after would look like this: Code: mce_b : { 'block' : 'mce_b' }, mce_i : { 'block' : 'mce_i' }, mce_u : { 'block' : 'mce_u' }, etc....... Notice the items I marked in red. Instead of these items having the VALUE of the variable "name", they instead have value of "name". That is, every part in red is "name". Any ideas how to make this work right (or an idea how to do it BETTER)? Thanks! -- Roger --Got it.
hello everyone im coding a website at the moment using aspx and i have came to a snag. im trying to make a dropdown list which is populated by an array call my chaletDetails.aspx page - each item in the array will be directed to this page. ive diffled abit, but the more i do the more im afraid of breaking something lol. if someone could maybe look at the code and show me how this could be achieved id appriciate it very much. The code i have for my dropdown list is as follows... this is in divResorts.js var resorts = new Array("Adelboden", "Auron", "Oppdal", "Las Lenas", "La Pierre St Martin", "Val Ceneis", "Les Menuires", "Champex-Lac", "Puy St Vincent"); function populateSelectList() { var mystring = document.getElementById('divResort').innerHTML mystring += "<h4>Our Resorts</h4><select name='resorts'>" mystring += "<option>Choose a Resort</option>" for (i = 0; i < resorts.length; i++) { mystring += "<option value='" + resorts[i] + "'>" mystring += resorts[i] + "</option>" } mystring += "</select>" mystring += "<h4>Our Chalets</h4>" document.getElementById('divResort').innerHTML = mystring } within the body i have this called as: <div id="divResort"> <script type="text/javascript">populateSelectList();</script> </div> new to JavaScript, simple question... question: how do i determine an output from a prompt to be bold? e.g. a user enters "hello" and the output printed from the prompt becomes bold one=parseFloat(prompt('enter number',0.bold)); I have a script that works good, but it says undefined underneath, this must mean an error. But I cant work out how to get rid of it. This is the html it is a transistional XHTML doc type. Code: <div id="slidedom"> <script type="text/javascript" src="js-files/slider.js"></div></script> Then I have the js file which I will have to post all of as I am not sure of the error bit. Code: var variableslide=new Array() //variableslide[x]=["path to image", "OPTIONAL link for image", "OPTIONAL text description (supports HTML tags)"] variableslide[0]=['images/sign-1.gif'] variableslide[1]=['images/sign-3.gif'] variableslide[2]=['images/sign-4.gif'] //configure the below 3 variables to set the dimension/background color of the slideshow var slidewidth='188px' //set to width of LARGEST image in your slideshow var slideheight='60px' //set to height of LARGEST iamge in your slideshow, plus any text description var slidebgcolor='#FFF' //configure the below variable to determine the delay between image rotations (in miliseconds) var slidedelay=3000 ////Do not edit pass this line//////////////// var ie=document.all var dom=document.getElementById for (i=0;i<variableslide.length;i++){ var cacheimage=new Image() cacheimage.src=variableslide[i][0] } var currentslide=0 function rotateimages(){ contentcontainer='<center>' if (variableslide[currentslide][1]!="") contentcontainer+='<a href="'+variableslide[currentslide][1]+'">' contentcontainer+='<img src="'+variableslide[currentslide][0]+'" border="0" vspace="3">' if (variableslide[currentslide][1]!="") contentcontainer+='</a>' contentcontainer+='</center>' if (variableslide[currentslide][2]!="") contentcontainer+=variableslide[currentslide][2] if (document.layers){ crossrotateobj.document.write(contentcontainer) crossrotateobj.document.close() } else if (ie||dom) crossrotateobj.innerHTML=contentcontainer if (currentslide==variableslide.length-1) currentslide=0 else currentslide++ setTimeout("rotateimages()",slidedelay) } if (ie||dom) document.write('<div id="slidedom" style="width:'+slidewidth+';height:'+slideheight+'; background-color:'+slidebgcolor+'"></div>') function start_slider(){ crossrotateobj=dom? document.getElementById("slidedom") : ie? document.all.slidedom : document.slidensmain.document.slidenssub if (document.layers) document.slidensmain.visibility="show" rotateimages() } if (ie||dom) start_slider() else if (document.layers) window.onload=start_slider I have taken out the credit, as I have changed it a bit, but will reinstate it later on. All I really need to know is how to define it in the XHTML maybe. Can anyone help? I'm working very hard to create a dynamic web-site for use in the education sector using server-side php/MySQL and client-side HTML with javascript. I have a logon page (index.php) which looks to see if the cookie 'mylogon' is set with the value 'Again'. This cookie is set when the PHP element on the server detects an invalid username/password combination. If so I have a javascript function (see below) call from the HTML <body onload="check_invalid_user()"> I know that the function Get_Cookie is working ok and that the javascript variable 'again' has the value 'Again' in it and has a length of 5. The alert function is not executed though. function check_invalid_user() { again = Get_Cookie('mylogon'); // document.write(again); // document.write(again.length); If (again == 'Again') { alert("User-id and/or password incorrect -- Please re-enter"); } } ============= Any help would be appreciated Many thanks in advance. (Yeah you guessed it I'm new and raw to this stuff. How to create a popup blocker with javascript? I am looking for solution to open Enquiry form [Ex: send-query.php] through javascript linking. How can I do that? And how to display a popup blocker message like in the attached file? This is the url of my pop up image: http://www.rhapsody.com/thompson-squ...mpson-square-2 [When you click on play button then if you have popup blocker enabled you will get popup blocker message from the site] Pretty pretty please help me with this I am running out of time and pulling my hair out with this whole thing. Thanks a million in advance! Rhonda <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> <script> function numberGuess() { stringVar=prompt("Enter your guess number"); while (stringVar<999) { } } </script> <script> function enterNames() { stringVar=prompt("Enter name"); } </script> <script> function enterProducts() { stringVar=prompt("Enter product name"); return } </script> </head> <body> <p> </p> <h1>WDV221 Intro Javascript</h1> <h3 align="center">Javascript Loops</h3> <p>Topics: loops, for loops, while loops, sentinels!</p> <p>Place all script code on this page.</p> <p>1. Create a function that will ask you for a number until you enter 999 which will end the loop. You do not need to display the number. </p> <p>2. Create a function that will ask you for a name until you tell the function that you do not have any more names. After you enter each name it will ask you if you have more names to enter. If you enter yes the process will continue. Display each name at the end of the following sentence.</p> <p>The name you entered is: </p> <p>3. Create a runtime script that will use a prompt( ) to accept how many products will be displayed. Use a for loop to display "Product Name" and the product number as a <p> element.</p> <p><strong>Example output:</strong></p> <p>Product Name 1</p> <p>Product Name 2</p> <p>...</p> <p>4. Create a runtime script that will use a nested for loop to create a table with 3 rows and 5 cells. Place the same number or a letter in each cell. </p> <p>Hint: Write a table in HTML. Do a row first then do multiple rows. </p> <p>5. Create a function called totalSales( ) that will ask the user to enter an amount until you enter "done". Add those amounts together and display them in an alert when the user is done entering amounts..</p> <p>Have fun!!</p> </body> </html> Hi, At www.happydaysremovals.com.estimatenew.html. Users fill in a form which has items of furniture, so they enter a number next to each item of furniture. The HTML uses text fields. When the form is submitted, the form is submitted to a php script that emails the results to myself. I am trying to also have a javascript function which runs before the php script runs. This javascript works out the cubic footage of all the items. If a user put "3" in the sofa text field, and I have defined that a sofa is 45 cubic feet, then the javascript will multiply 3 * 45. It will do a similar thing for all items of furniture, then add them all up to give a total cubic feet. I want that total field to then sent as a variable to the php script, along with all the other variables. My code so far (which doesnt work) Code: <script language="JavaScript"> function calculate() { var sofa_3_seater = document.getElementById('sofa_3_seater').value*45; var sofa_2_seater = document.getElementByID('sofa_2_seater').value*30; var armchair_large = document.getElementByID('armchair_large').value*15; var total=sofa_3_seater+sofa_2_seater+armchair_large; document.getElementByID('total').value = total; } </script> I have used a hidden field for the total value: Code: <input type="hidden" name="total" value=""> I want the javascript result to change the value of the hidden field that is called total. THen I want it all to be posted to the PHP script and email everything to me, including this newly calculated total field! THanks I have a page with options, and I need to hide options unless a certain choice is selected. So say you have a select box for male and female, if they select male, I need it to show male options, and if they select female, it shows female options. How can I hide things until selected and then show the textboxes and stuff? Thank you! Please see the below code. Is it possible for me to type in an order date in the browser & in the due date field, have it automatically populate a date 7 days greater than the date typed in the order date field? If so how? I have read I will probably need javascript to do this but have no clue where to start. Thanks for any help provided! <html> <head> <title> Update Database </title> </head> <body> <form method="post" action="add.php"> <b>First Name:</b> <br /> <input type="text" name="customer_fname" size="35" /><br /> <b>Last Name:</b> <br /> <input type="text" name="customer_lname" size="35" /><br /> <b>Location:</b> <br /> <input type="text" name="location" size="35" /><br /> <b>Order Date:</b> <br /> <input type="text" name="order_date" size="35" /><br /> <b>Due Date:</b> <br /> <input type="text" name="due_date" size="35" /><br /> <input type="submit" value="Submit" /> </form> </body> </html> i have this line of code: Code: <a href="#" id="profilelink" name="link2" onClick="viewornot(<?php echo $freechat_id ?>)"><?php echo $freechat_list; ?></a> //call the JS onclick and if OK was click do the message box and send button here then i have my JS: Code: function viewornot(id) { var e = confirm('Do you want to view this profile?'); if (e == true) { window.location.href = "http://www-rainbowcode-net/apps_dev.php/profiles/showprofilepersonal?id="+id; window.location('http://www-rainbowcode-net/apps_dev.php/profiles/showprofilepersonal?id='+id); return true; } else { var e2 = confirm('Do you want to send a message?'); if (e2 == true) { //if ok was clicked send value back?? return e2; } } } on the e2 confirm: if i clicked OK how can i send a value back(so that i know OK was clicked) so that i can then produce the message box and send button and do the rest of the code to send a message??? thank you Hello everyone, i desperatly need a script for my website but am totally stuck, I hope someone can help On my website i want a search box. I want it so that if a use types in one of my predetermined search terms then they would be sent to a predeterimed page in my own website. For example: I have a group of 5 keywords - nokia, mobile, vodafone, cellphone, iphone. If a user types any of those keywords into my search box then xxxxx.com/mobilephones.htm will be loaded up I have another set of 5 keywords - cat, dog, rabbit, mouse, snake. If a user types any of those into the search box then they are directed to xxxxx.com/animals.htm and so on ... So as you can see, its not a 'normal' search engine I will be creating new pages and will need to add the chosen keywords for it as time goes on etc Nearly everywhere i look for something to help me all I can find are standard search engine scripts which are no good as they display search results instead of directing to specific urls depending on the keywords entered I would also need to 'capture' what search terms are being entered so that I can build some user statistics I understand html and javacript to an intermediate level Can anyone help / provide a script that I could use? Although Im a student Im willing to pay some funds to anyone that can do this for me Many thanks Terry Hi, I'm trying to get my head around JS, but not too successfully yet. My objective is probably simple (but not to me ): to have a form where visitors enter 12 separate digits (ideally in minutes & hours, but am happy to use decimals), and the average of these (to two decimal points) is returned as document.write is it? - along with other text, to make a complete & coherent sentence, including the average of the 12 numbers. Does anyone have any code convenient that could do this? Thanks, LJ I tried to write the variable my_var into a text file but it only shows null after executing the code. Can anybody help pls?? Below is my code <html> <head> <script language="javascript"> var my_var function WriteToFile() { my_var = 123; document.write(<?php $file="file.txt"; $fh = fopen($file, 'w') or die("can't open file"); ?>; document.write(<?php $stringData = my_var ?>; document.write(<?php fwrite($fh, $stringData); ?>; document.write(<?php fclose($fh); ?>; } </script> </head> <body onLoad="WriteToFile();"> <p>Hello World</p> </body> </html> I am still learning javascript so forgive me if this question is a bad one, I am making scripts for Greasemonkey for a website I am a part of. One of the developers recently commented out (in the source code) a feature that was available before. It is a Football MMORPG game and it used to show college players 40 times but over the past month he commented out the tag that held the script function. I want to know if i can either A) write a js code that can remove the comment tag in the source code or B) write a code that will just add another tag under that and have that js function that was commented out valid. I tried something but ended up just having that function pasted onto the page instead of having it put into the source to have it read by the rest of the data.. here is what im talking about <TABLE width=800 cellspacing=0> <tr> <td colspan=3 style="FONT-SIZE: 11pt"> <b>Hometown:</b>Fort Hunt, VA<br> <b>Height:</b>6-0<br> <b>Weight:</b>206<br> <!--b>40 Time:</b><if rs("team_id")>0 or bRated then><=formatnumber(rs("forty"),2)><br><end if --> i just want to see if there was any way to just delete the comment out of it so i can get this feature again is all. Any help would be awesome... and sorry for the long post |