JavaScript - Multiple Radio Button Field Form Validation
I'm only validating one (Consent) radio button with this code but i need to validate multiple different questions/buttons.
Code: <script> function getRBtnName(GrpName) { var sel = document.getElementsByName(GrpName); var fnd = -1; var str = ''; for (var i=0; i<sel.length; i++) { if (sel[i].checked == true) { str = sel[i].value; fnd = i; } } return fnd; } function checkForm() { var chosen = getRBtnName('Consent'); if (chosen < 0) { alert( "Please choose one answer when you are asked to select a number." ); return false; } else { return true; } } </script> <form action="congratulations_aff.php" method="post" name="congratulations_aff" onSubmit="return checkForm()"> <table border="0" cellspacing="1" cellpadding="0"> <tr> <td colspan="3">I consent to providing my electronic signature.</p></td> </tr> <tr> <td colspan="3" valign="top"> <input type="radio" name="Consent" value="Y" /> Yes <input type="radio" name="Consent" value="N" /> No <table border="0" cellspacing="1" cellpadding="0"> <tr> <td>I consent to electronic receipt of my information reporting documentation.</td> </tr> <tr> <td valign="top"> <input type="radio" name="Consent1099YesNo" value="Y" /> Yes <input type="radio" name="Consent1099YesNo" value="N" /> No</td> </tr> <tr> <td valign="top"> For tax purposes are you a U.S. citizen, U.S. resident, U.S. partnership, or U.S. corporation? <input type="radio" name="USPersonYesNo" value="Y" /> Yes <input type="radio" name="USPersonYesNo" value="N" /> No</tr> </table> <input type="submit" value="submit" value="Submit" /> </form> Any help would be greatly appreciated. Similar TutorialsHi, i'm new here and I need help. I need a script that can alert in 2 ways for multiple radio button groups of "yes" and "no." I got the first question to alert the way I need (with alert #1 for yes and alert#2 for no.) However, starting from the 2nd question it alerts #1 for both yes and no. This is the script I'm working with: Code: <script type="text/javascript"> function valbutton(form) { myOption = -1; for (i=form.buttonset1.length-1; i > 0; i--) { if (form.buttonset1[i].checked) { myOption = i; i = -1; } } if (myOption == -1) { alert("Please select Yes or No"); return false; } if (myOption == 1) { alert("Yes, it works!"); return false; } if (myOption == 2) { alert("No, it won't work."); return false; } } </script> The following are the 1st two sets of radio buttons: Code: <form name="myform" action="formstest.html"> <tr> <td> <input type="reset" value="Click to Reset"> <br><input type="radio" name="buttonset1" value="1" rel="none" id="r1" /><label for="r1">Click to Start Over</label><br /></td> </tr> <tr><td class="question">Question #1</td> <td><input type="radio" name="buttonset1" value="2" rel="flat" id="r2" /> <label for="r2">Yes</label><br /> <input type="radio" name="buttonset1" value="3" rel="none" id="r3" /> <label for="r3">No</label><br /> </td> </tr> <tr rel="flat"> <td class="question"><label for="flat"><span class="accessibility">If metal:</span>Question #2</label></td> <td><input type="radio" name="buttonset2" value="4" rel="uneven" id="r4" /> <label for="r4">Yes</label><br /> <input type="radio" name="buttonset2" value="5" rel="none" id="r5" /> <label for="r5">No</label><br /> </td> </tr> Is it possible to switch out the alerts for yes and no for next 4 questions? And finally, the following is the lengthy, thorough version of my question if it clarifies what I'm trying to do. I would really appreciate someone to point me the right direction. I've tried all kinds of variation to the script and I just can't seem to make it work. Thanks in advance! (Lengthy Version) I also need questions to appear (or not show) below as select radio buttons are clicked. I have 6 groups of yes and no radio buttons. I would like the first two groups to have "alert 1" when yes is clicked (and "alert 2" for no.) I would like the rest to show "alert 2" when yes is clicked (and "alert 1" for no.) (I also have all the questions hidden below question 1. Each subsequent questions appear upon clicking yes for questions 1&2, and no for the rest. I would like to keep this intact - I put that js code I downloaded at the end of this post.) Here's what I have so far... I got most of it working except after the first question, "alert 1" appears for all the clicks submitted. I've been at it for a few weeks and I can't seem to figure it out. Thanks in advance! Code: <html> <head> <title>Form Test</title> <script type="text/javascript"> function valbutton(form) { myOption = -1; for (i=form.buttonset1.length-1; i > 0; i--) { if (form.buttonset1[i].checked) { myOption = i; i = -1; } } if (myOption == -1) { alert("Please select Yes or No"); return false; } if (myOption == 1) { alert("Yes, it works!"); return false; } if (myOption == 2) { alert("No, it won't work."); return false; } } </script> </head> <body> <form name="myform" action="formstest.html"> <tr> <td> <input type="reset" value="Click to Reset"> <br><input type="radio" name="buttonset1" value="1" rel="none" id="r1" /><label for="r1">Click to Start Over</label><br /></td> </tr> <tr><td class="question">Question #1</td> <td><input type="radio" name="buttonset1" value="2" rel="flat" id="r2" /> <label for="r2">Yes</label><br /> <input type="radio" name="buttonset1" value="3" rel="none" id="r3" /> <label for="r3">No</label><br /> </td> </tr> <tr rel="flat"> <td class="question"><label for="flat"><span class="accessibility">If metal:</span>Question #2</label></td> <td><input type="radio" name="buttonset2" value="4" rel="uneven" id="r4" /> <label for="r4">Yes</label><br /> <input type="radio" name="buttonset2" value="5" rel="none" id="r5" /> <label for="r5">No</label><br /> </td> </tr> <tr rel="uneven"> <td class="question"><label for="uneven"><span class="accessibility">If flat:</span> Question #3</label></td> <td><input type="radio" name="buttonset3" value="6" rel="none" id="r6" /> <label for="r6">Yes</label><br /> <input type="radio" name="buttonset3" value="7" rel="ridges" id="r7" /> <label for="r7">No</label><br /> </td> </tr> <tr rel="ridges"> <td class="question"><label for="ridges"><span class="accessibility">If uneven:</span> Question #4</label></td> <td><input type="radio" name="buttonset4" value="8" rel="none" id="r8" /> <label for="r8">Yes</label><br /> <input type="radio" name="buttonset4" value="9" rel="holes" id="r9" /> <label for="r9">No</label><br /> </td> </tr> <tr rel="holes"> <td class="question"><label for="holes"><span class="accessibility">If ridges:</span> Question #5</label></td> <td><input type="radio" name="buttonset5" value="10" rel="none" id="r10" /> <label for="r10">Yes</label><br /> <input type="radio" name="buttonset5" value="11" rel="curved" id="r11" /> <label for="r11">No</label><br /> </td> </tr> <tr rel="curved"> <td class="question"><label for="curved"><span class="accessibility">If holes:</span>Question #6 </label></td> <td><input type="radio" name="buttonset6" value="12" rel="none" id="r12" /> <label for="r12">Yes</label><br /> <input type="radio" name="buttonset6" value="13" rel="answer" id="r13" /> <label for="r13">No</label><br /> </td> </tr> <tr rel="answer"> <td></td> <td class="question"><label for="answer"><span class="accessibility">If not curved:</span><strong> Yes, correct answer!</strong></label></td> </tr> <tr> <td colspan="2"><input type="submit" name="submitit" onclick="valbutton(myform);return false;" value="Submit" /></td> </tr> </tbody> </table> </form> </body> </html> The following is the JS found to make corresponding questions appear as each relevant radio button answer is clicked: Code: /*****************************************/ /** Usable Forms 2.0, November 2005 **/ /** Written by ppk, www.quirksmode.org **/ /** Instructions for use on my site **/ /** **/ /** You may use or change this script **/ /** only when this copyright notice **/ /** is intact. **/ /** **/ /** If you extend the script, please **/ /** add a short description and your **/ /** name below. **/ /*****************************************/ var containerTag = 'TR'; var compatible = ( document.getElementById && document.getElementsByTagName && document.createElement && !(navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1) ); if (compatible) { document.write('<style>.accessibility{display: none}</style>'); var waitingRoom = document.createElement('div'); } var hiddenFormFieldsPointers = new Object(); function prepareForm() { if (!compatible) return; var marker = document.createElement(containerTag); marker.style.display = 'none'; var x = document.getElementsByTagName('select'); for (var i=0;i<x.length;i++) addEvent(x[i],'change',showHideFields) var x = document.getElementsByTagName(containerTag); var hiddenFields = new Array; for (var i=0;i<x.length;i++) { if (x[i].getAttribute('rel')) { var y = getAllFormFields(x[i]); x[i].nestedRels = new Array(); for (var j=0;j<y.length;j++) { var rel = y[j].getAttribute('rel'); if (!rel || rel == 'none') continue; x[i].nestedRels.push(rel); } if (!x[i].nestedRels.length) x[i].nestedRels = null; hiddenFields.push(x[i]); } } while (hiddenFields.length) { var rel = hiddenFields[0].getAttribute('rel'); if (!hiddenFormFieldsPointers[rel]) hiddenFormFieldsPointers[rel] = new Array(); var relIndex = hiddenFormFieldsPointers[rel].length; hiddenFormFieldsPointers[rel][relIndex] = hiddenFields[0]; var newMarker = marker.cloneNode(true); newMarker.id = rel + relIndex; hiddenFields[0].parentNode.replaceChild(newMarker,hiddenFields[0]); waitingRoom.appendChild(hiddenFields.shift()); } setDefaults(); addEvent(document,'click',showHideFields); } function setDefaults() { var y = document.getElementsByTagName('input'); for (var i=0;i<y.length;i++) { if (y[i].checked && y[i].getAttribute('rel')) intoMainForm(y[i].getAttribute('rel')) } var z = document.getElementsByTagName('select'); for (var i=0;i<z.length;i++) { if (z[i].options[z[i].selectedIndex].getAttribute('rel')) intoMainForm(z[i].options[z[i].selectedIndex].getAttribute('rel')) } } function showHideFields(e) { if (!e) var e = window.event; var tg = e.target || e.srcElement; if (tg.nodeName == 'LABEL') { var relatedFieldName = tg.getAttribute('for') || tg.getAttribute('htmlFor'); tg = document.getElementById(relatedFieldName); } if ( !(tg.nodeName == 'SELECT' && e.type == 'change') && !(tg.nodeName == 'INPUT' && tg.getAttribute('rel')) ) return; var fieldsToBeInserted = tg.getAttribute('rel'); if (tg.type == 'radio') { removeOthers(tg.form[tg.name],fieldsToBeInserted) intoMainForm(fieldsToBeInserted); } } function removeOthers(others,fieldsToBeInserted) { for (var i=0;i<others.length;i++) { var show = others[i].getAttribute('rel'); if (show == fieldsToBeInserted) continue; intoWaitingRoom(show); } } function intoWaitingRoom(relation) { if (relation == 'none') return; var Elements = hiddenFormFieldsPointers[relation]; for (var i=0;i<Elements.length;i++) { waitingRoom.appendChild(Elements[i]); if (Elements[i].nestedRels) for (var j=0;j<Elements[i].nestedRels.length;j++) intoWaitingRoom(Elements[i].nestedRels[j]); } } function intoMainForm(relation) { if (relation == 'none') return; var Elements = hiddenFormFieldsPointers[relation]; for (var i=0;i<Elements.length;i++) { var insertPoint = document.getElementById(relation+i); insertPoint.parentNode.insertBefore(Elements[i],insertPoint); if (Elements[i].nestedRels) { var fields = getAllFormFields(Elements[i]); for (var j=0;j<fields.length;j++) { if (!fields[j].getAttribute('rel')) continue; if (fields[j].checked || fields[j].selected) intoMainForm(fields[j].getAttribute('rel')); } } } } function getAllFormFields(node) { var allFormFields = new Array; var x = node.getElementsByTagName('input'); for (var i=0;i<x.length;i++) allFormFields.push(x[i]); var y = node.getElementsByTagName('option'); for (var i=0;i<y.length;i++) allFormFields.push(y[i]); return allFormFields; } /** ULTRA-SIMPLE EVENT ADDING **/ function addEvent(obj,type,fn) { if (obj.addEventListener) obj.addEventListener(type,fn,false); else if (obj.attachEvent) obj.attachEvent("on"+type,fn); } addEvent(window,"load",prepareForm); /** PUSH AND SHIFT FOR IE5 **/ function Array_push() { var A_p = 0 for (A_p = 0; A_p < arguments.length; A_p++) { this[this.length] = arguments[A_p] } return this.length } if (typeof Array.prototype.push == "undefined") { Array.prototype.push = Array_push } function Array_shift() { var A_s = 0 var response = this[0] for (A_s = 0; A_s < this.length-1; A_s++) { this[A_s] = this[A_s + 1] } this.length-- return response } if (typeof Array.prototype.shift == "undefined") { Array.prototype.shift = Array_shift } 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 Wondering how come this doesn't work any ideas? its validating that the radio buttons have been selected. Code: if ( ( form.q1[0].checked == false ) && ( form.q2[1].checked == false) && ( form.q3[2].checked == false )) { alert ( "Please Choose a " ); return false; } Hi everyone. I'm getting into javascript coding and i'm stumped with some problem about radio buttons. What I would like is to have a general function where I can pass in any group of radio buttons and have it return the textual value of the radio button selected. First, here is my code so far. Code: function Get_Radio_Selection(options) { var choice = ""; for (var index = 0; index <= options.length; index++) if (options[index].checked == true) choice = options[index].value; return choice; } Now here's a sample form: Code: <form name="form1"> <input type="radio" name="food" value="Banana">Banana <input type="radio" name="food" value="Mango">Mango <input type="radio" name="food" value="Orange">Orange <input type="radio" name="food" value="Oatmeal">Oatmeal <input type="button" name="blah" value="Get Selection" onclick="Get_Radio_Selection(document.form1.food);"> </form> My question is, when i run this function and pass it in the group of radio buttons by means of the onclick event handler, firefox 3.6.22 reports in the error console on line 22 that "options[index] is undefined". It seems that if i use the variable "index" more than one time it reports this error but if I take out the assignment to variable "choice" that it works just fine. I rewrote the function using a separate variable and this time it works perfectly: Code: function Get_Radio_Selection(options) { var choice = ""; var index; for (var i = 0; i < options.length; i++) { if (options[i].checked == true) { index = i; choice = options[index].value; } } return choice; } Notice the indexing variables in the "if" statement and the assignment to "choice" are different. Does anyone have any suggestions on why this happens? Thanks. Hi hope someone here can help me with a problem i'm stuck on. Basically I have 3 radio buttons and one of them has to be selected otherwise display a error message but I cant figure it out. Here is my failed attempt: Code: <html> <head> <link rel="stylesheet" type="text/css" href="css/style.css" /> <script type="text/javascript"> function validateForm() { var x=document.forms["form1"]["name"].value; var y=document.form1.difficulty.value; if (x==null || x=="") { alert("First name must be filled out"); return false; } if (y==false) { alert(y); return false; } } </script> </head> <body> <div id="horizon"> <div id="content"> <img src="images/logo.jpg" width="649" height="410"></div> <div id="content2"><form name ="form1" method ="post" action ="game.php" onSubmit="return validateForm()" > Name: <input type ='text' name='name' /> <br /> <Input type = 'Radio' Name ='difficulty' value= '1' />Easy <Input type = 'Radio' Name ='difficulty' value= '2' />Medium <Input type = 'Radio' Name ='difficulty' value= '3' />Hard <br /><Input type = "Submit" Name = "Submit" VALUE = "Submit"> </form></div></div> </body> </html> Thanks in advance how would i check to see if my radio button is not checked. I know in javascript the term "checked" works. Is there a way to us unchecked? here is what i am trying to figure out. Code: with (thisform) { if(document.getElementById('m21').unchecked) { window.alert('Please Verify Your Age') m21.focus(); return false;} } go he http://qodeplay.com/javascript help me accomplish this and send this data to a mysql column! note: the field will not grey out in ie. Hello All. What my script does is if you do not select a radio button and you hit submit an error will pop up saying please select game1. this is taken from the name of the radio button.. How can i make it so it prints out the VALUES of the 2 radio buttons. end result should print please select Baltimore Ravens vs. Cincinnati Bengals rather than please select game1. Code: function isChecked(radgrp) { var i = radgrp.length; do if (radgrp[--i].checked) return true; while (i); return false; } function validateTest(els) { var focus_me = null, msg = ""; if (!isChecked(els.game1)) { msg += " Game #1\n"; focus_me = focus_me || els.game1[0]; } if (!isChecked(els.game2)) { msg += " Game #2\n"; focus_me = focus_me || els.game2[0]; } if (!isChecked(els.game16)) { msg += " Game #16\n"; focus_me = focus_me || els.game16[0]; } if (msg != "") { var prefix = "\n WARNING: The following Games(s) were not selected:\n\n"; var suffix = "\nClick OK to submit your picks anyway.\n\n"; var suffix = suffix + "\n Click CANCEL to correct your picks." var ask = confirm(prefix + msg + suffix); if (ask) { if (focus_me) focus_me.focus(); return true; } else{ return false; } } } Here is the radio button. If you dont select the radio button i want the javascript validation pop but to say "please select Baltimore Ravens vs. Cincinnati Bengals rather than game1 like it does now. Code: <INPUT TYPE=RADIO NAME="game1" VALUE="Baltimore Ravens">Baltimore Ravens<BR> <INPUT TYPE=RADIO NAME="game1" VALUE="Cincinnati Bengals">Cincinnati Bengals<BR> Thanks Hi, I'm having a problem trying to get my html web form to work. I have a list of 8 radio button options for people to choose one option with the last option being other - and then I want if the user clicks other that the text field box next to it becomes a required field and the form will not submit until this text field is filled in. If the user selects one of the other radio button options then the text field is not compulsory. I am using a linked javascript validation file which checks that all fields on the form which I have stated as required - become required, but I am having problems getting this "Other" button and text field to work with it all and not have the submit continue without this text field being completed! any help would really be appreciated! Thanks in advance! I am using ASP validators and I have a contact form. I want to be able to have a phone and email radio button group. The email textbox also has a RegularExpressionValidator If the phone item is selected then I want the validation to be enabled on the phone text box making it mandatory while the email text box isn't, and if they choose the email as the contact it will be reversed. I want to be able to do this without having to do a postback. I already have the logic on the code behind and the enquiry object. also I am fairly new to javascript so I have been using mostly jQuery as easier to implement 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> I have three radio button groups with different values. My script only works for one group. Please help! <html xmlns="http://www.w3.org/1999/xhtml"> <head> <script> function emailTo(){ var emails = document.getElementsByName("email1"); var emailAdress = null; for(var i = 0; i < emails.length; i++){ if(emails[i].checked){ emailAdress = emails[i].value; } } if(emailAdress == null){ alert("select the email address"); return; } var mailto_link = 'mailto:'+emailAdress; var win = window.open(mailto_link,'emailWindow'); if (win && win.open &&!win.closed) win.close(); } </script> </head> <body> <p> <input type = "radio" name = "email1" value = "email1.com"/> recipient 1a <input type = "radio" name = "email1" value = "email1.com"> recipient 1b </p> <p> <input type = "radio" name = "email2" value = "email2.com"/> recipient 2a <input type = "radio" name = "email2" value = "email2.com"> recipient 2b</p> <p> <input type = "radio" name = "email3" value = "email3.com"/> recipient 3a <input type = "radio" name = "email3" value = "email3.com"> recipient 3b</p> <p> </p> <p> <input type = "button" onClick = "emailTo()" value = "Email"/> </p> </body> </html> I need a script that will allow a user to select a vertical selection and Horizontal selection that displays a corresponding image. There are only 8 combinations (4 vertical choices and 2 horizontal choices) I just need to display the correct image and a description. There has to be only one of each selected and only display one image. I have tried several and have had no luck. Any ideas? Here is how the layout will look I've set up a mock registration form page so I can learn a bit about javascript's form validation. (newbie) I want to try to attempt to style the border of a form field green when the user enters the correct info into the form text field and red on all other fields if the user doesnt enter any info into them. When i test it, enter the right info into the username field, leave the others blank, and hit the submit button it styles the username field green ok but it doesnt make the next fields (password and so on) red. Could someone please explain what I am doing wrong? here is my code so far... Note: just for testing purposes I've put return false on everything so it displays a message when everythings ok. Code: .... <script type="text/javascript"> window.onload = function() { document.forms[0].username.focus(); } function validate(form) { var form = document.getElementById("reg"); var e = document.getElementById("error"); e.style.background = "red"; for(var i = 0; i < form.elements.length; i++) { var el = form.elements[i]; if(el.type == "text" || el.type == "password") { if(el.value == "") { e.innerHTML = "Please fill in all fields!"; el.style.border = "1px solid red"; el.focus(); return false; } else { el.style.border = "1px solid green"; return false; } } } var un = form.username; un.value = un.value.replace(/^\s+|\s+$/g,""); if((un.value.length < 3)|| (/[^a-z0-9\_]/gi.test(un.value))) { e.innerHTML = "Only letters,numbers and the underscore are allowed (no spaces) - 3-16 characters"; un.focus(); return false; } var pw = form.password; pw.value = pw.value.replace(/^\s+|\s+$/g,""); if((pw.value.length < 3) || (/[^a-z0-9]/gi.test(pw.value))) { e.innerHTML = "Only letters and numbers are allowed (no spaces) - 3-16 characters"; pw.focus(); return false; } e.innerHTML = "You filled in all the fields, well done!"; e.style.background = "green"; return false; } </script> </head> <body> <div id="wrapper"> <div id="header"> <h1>My Cool Website</h1> </div> <div id="content"> <div class="padding"> <h2>Registration</h2> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Duis ligula lorem, consequat eget, tristique nec, auctor quis, purus. Vivamus ut sem. Fusce aliquam nunc vitae purus. Aenean viverra malesuada libero. Fusce ac quam. Donec neque. Nunc venenatis enim nec quam. Cras faucibus, justo vel accumsan aliquam, tellus dui fringilla quam, in condimentum augue lorem non tellus. Pellentesque id arcu non sem placerat iaculis. Curabitur posuere, pede vitae lacinia accumsan, enim nibh elementum orci, ut volutpat eros sapien nec sapien. Suspendisse neque arcu, ultrices commodo, pellentesque sit amet, ultricies ut, ipsum. Mauris et eros eget erat dapibus mollis. Mauris laoreet posuere odio. Nam ipsum ligula, ullamcorper eu, fringilla at, lacinia ut, augue. Nullam nunc.</p> <form id="reg" action="#" method="post" onsubmit="return validate(this)"> <div id="error"></div> <div><label for="username">Username</label></div> <div><input type="text" name="username" id="username" size="30" maxlength="16" /></div> <div><label for="password">Password</label></div> <div><input type="password" name="password" id="password" size="30" maxlength="16" /></div> <div><label for="c_password">Confirm Password</label></div> <div><input type="password" name="c_password" id="c_password" size="30" maxlength="16" /></div> <div><label for="email">Email address</label></div> <div><input type="text" name="email" id="email" size="30" maxlength="200" /></div> <div><label for="c_email">Confirm Email address</label></div> <div><input type="text" name="c_email" id="c_email" size="30" maxlength="200" /></div> <div><label for="firstname">First name</label></div> <div><input type="text" name="firstname" id="firstname" size="30" maxlength="100" /></div> <div><label for="surname">Surname</label></div> <div><input type="text" name="surname" id="surname" size="30" maxlength="100" /></div> <div><label for="gender">Gender</label></div> <div> <div><input type="radio" name="gender" id="gender" value="m" checked="checked" />Male</div> <div><input type="radio" name="gender" value="f" />Female</div> </div> <div><input type="submit" value="Register" name="submit" /></div> </form> </div> </div> <div id="footer"> <p>Copyright © 2009 My Cool Website</p> </div> </div> </body> </html> Hello everyone. I'm new to programming, although I've learned HTML and CSS pretty thoroughly. I think I have most of the Javascript basics down as far as definitions, but I'm getting hung up on the actual coding. I'm working on an information request form for my fiancee's business site. In it, I want to have options that users select via radio buttons. Rather than having additional input fields static on the page, I would like to program some of the radios, when clicked, to open a previously unseen option where users will then be able to type in the necessary information. More specifically, since she only works in two states, I want a third option of "other," which, when opened, will allow the user to input any other state. Then, should the radio be deselected or reset, the field would disappear again. If my understanding is correct, I think I will need to use a javascript function in the header, an HTML "div" section with ID, "onclick" and "offclick" events in the button mark-up, and the specific javascript code to call the function (getElementById?). Or maybe I'm way off. Please help. Thanks I am trying to build a page that has a form on it with three questions, each having two answers via radio button. When the user hits submit, depending on their answers to the three questions, it redirects them to a different page. This is all I have so far and am stuck, I dont know much JS. <script type="text/javascript"> function whereTo () { if ((Goods==true) && (Direct==true) && (SoleSource==true)) { window.location = "http://www.somesite/page1"; } if ((Goods==true) && (Direct==true) && (NetworkID==true) && (Competed==true)) { window.location = "http://www.somesite/page2"; } if ((Goods==true) && (Indirect==true) && (CostCenter==true) && (SoleSource==true)) { window.location = "http://www.somesite/page3"; } if ((Goods==true) && (Direct==true) && (NetworkID==true) && (SoleSource==true)) { window.location = "http://www.somesite/page4"; } if ((Goods==true) && (Indirect==true) && (CostCenter==true) && (Competed==true)) { window.location = "http://www.somesite/page5"; } if ((Goods==true) && (Direct==true) && (NetworkID==true) && (Competed==true)) { window.location = "http://www.somesite/page6"; } } </script> The form (it only has the inputs not the questions, i know its not setup properly, just wanted to show the inputs): <form> <input type="radio" id="Goods" name="good" /> Goods<br /><input type="radio" id="Services" name="Services" /> Services <input type="radio" id="Direct" name="good" /> Direct<br /><input type="radio" id="Indirect" name="Indirect" /> Indirect <input type="radio" id="CostCenter" name="CostCenter" /> Cost Center<br /><br /><input type="radio" id="NetworkID" name="NetworkID" /> Network ID <input type="radio" id="SoleSource" name="order" /> Sole Source<br /><input type="radio" id="Competed" name="order" /> Can Be Competed</td> <input alt="Submit" onclick=" return whereTo()" /> </form> I have 4 rows in a table. Each row consist of 4 radio buttons. Each radio button per row has different values in it but has the same name (group) ex: <input type="radio" name="a" value="1"> <input type="radio" name="a" value="2"> <input type="radio" name="a" value="3"> <input type="radio" name="a" value="4"> <input type="radio" name="b" value="1"> <input type="radio" name="b" value="2"> <input type="radio" name="b" value="3"> <input type="radio" name="b" value="4"> and so on.. If I click radio button A with value 2, I want to output the total at the bottom as "2".. Also, if I click radio button B with value 3, I want to output the total of A and B as 5 and so on.. How can I automatically calculate the answer based on which radio button was click? update: I got my answer from this site: http://stackoverflow.com/questions/1...s-using-jquery Hello all, I have 2 radio buttons update bsc value update mnr value when user selects update mnr value, two more radio buttons should appear. I have mnr value I do not have mnr value. On selecing I do not have mnr value im submitting the form and enabling the user to select values from dropdown. ( not shown in the code as it is a coldfusion code.). On submit I am not able to still keep the 'but2', and 'my_select3' elements disabled. Please advice. I have been working on this since quite sometime. <script language="JavaScript"> function makeChoice() { var obj1 = document.getElementById('box1') var obj2 = document.getElementById('box2') if (document.getElementById('but1').checked) { obj2.setAttribute('readOnly','readonly'); obj1.removeAttribute('readOnly'); document.getElementById('but1').disabled=true; document.getElementById('but2').disabled=true; } else if (document.getElementById('but2').checked) { obj1.setAttribute('readOnly','readonly'); obj2.removeAttribute('readOnly'); document.getElementById('but2').disabled=true; document.getElementById('but1').disabled=true; } } </script> <script type="text/javascript"> function showSelect() { if (document.getElementById('but2').checked) { document.getElementById('but2').disabled=true; } } </script> <script type="text/javascript"> function showSelect1() { if (document.getElementById('but2').checked) { document.myform.submit(); document.getElementById('but2').disabled=true document.getElementById('my_select3').disabled=true } } </script> <td><input type="radio" id="but1" name="vals" onclick="makeChoice()"></td> <td>Update BSC Value </td> <tr> <td><input type="radio" id="but2" name="vals" onclick="makeChoice();showSelect();" ></td> <td>Update MNR Value </td> <td> <input id="my_select1" class="show" type="radio" name="my_select1" onclick="this.myform['my_select1'].disabled=true"></td> <td> I have a Position Number </td> <td><input id="my_select3" class="show" type="radio" name="my_select3" onclick="showSelect1();"></td> <td> I do not have a Position Number </td> Hope someone can help. i have this script I found that i think does what i am looking for but i cant seem to get it to work.I think I am just overlooking something. I have a group of radio buttons in a form on a page and i am trying to get the value and image attached to the chosen radio button to pass to another form on another page.here is what i have so far. form 1 page named - giftcards.html Code: <form action="GIFTCERTIFICATES.html" name="giftcardstyle"> <input name="giftcardstocknumber" type="radio" value="BBA - 006"/> <img src="GIFTCARDS/004 BASIC4.jpg" alt="bba-006" style="width: 150px; height: 100px"> form 2 new page named - giftcertificates.html Code: <SCRIPT LANGUAGE="JavaScript" type="text/javascript"><!-- function getParm(string,parm) { // returns value of parm from string var startPos = string.indexOf(parm + "="); if (startPos > -1) { startPos = startPos + parm.length + 1; var endPos = string.indexOf("&",startPos); if (endPos == -1) endPos = string.length; return unescape(string.substring(startPos,endPos)); } return ''; } var passed = location.search.substring(1); document.f1.giftcardstocknumber.value = getParm(passed,'giftcardstocknumber'); document.formname.otherfield.value = getParm(passed,'otherfield'); //--></SCRIPT> <input type="text" name="giftcardstocknumber"/> <img src="" style="width: 150px; height: 100px"/> |