TVanBusk
01-05-2004, 01:06 PM
I have found several scripts that validate a text field on a form, but I need to validate a radio button ... where the user cannot submit the form until one button or the other is clicked.
|
Click to See Complete Forum and Search --> : Validation for radio buttons TVanBusk 01-05-2004, 01:06 PM I have found several scripts that validate a text field on a form, but I need to validate a radio button ... where the user cannot submit the form until one button or the other is clicked. olerag 01-05-2004, 01:22 PM Why don't you simply activate one of the buttons on page load as the default. Then, you'll always have one of the buttons selected. TVanBusk 01-05-2004, 01:28 PM I thought of that too, but the two options are so totally opposite, that people may not get what they expect. It is to either activate or disconnect services. olerag 01-05-2004, 01:40 PM OK then, One more thought - do you want to use two separate checkboxes instead of a radio group? If not, here's some code that will make sure at least one of the RG's is selected..... <script type="text/javascript"> function validate(form) { var rgObj = form.rg1; if ( (!rgObj[0].checked) && (!rgObj[1].checked) ) alert("Fail"); else alert("Pass"); } </script> HTML is.. <form> <input type="radio" name="rg1" value="1">Value 1 <input type="radio" name="rg1" value="2">Value 2 <p> <input type="button" name="btn1" value="Proceed" onClick="validate(this.form)"> </form> TVanBusk 01-05-2004, 02:23 PM Thanks so much ... we're going to give that a whirl! jtf 03-06-2004, 06:10 PM Hi So how would I go about altering that script for multiple buttons. <script type="text/javascript"> function validate(form) { var rgObj = form.rg1.rg2.rg3; if ( (!rgObj[0].checked) && (!rgObj[1].checked) ) alert("Fail"); else alert("Pass"); } </script> Or would I go <script type="text/javascript"> function validate(form) { var rgObj = form.(); if ( (!rgObj[0].checked) && (!rgObj[1].checked) ) alert("Fail"); else alert("Pass"); } </script> thanks for your help in advance steelersfan88 03-06-2004, 07:47 PM This is a totally new approach, but it will do:<script type="text/javascript"> function validate() { var theCon = "" var Obj = new Array Obj[0] = "0,1" //element numbers that equal the grouped radios, with same name Obj[1] = "3,4,5" //element numbers that equal the grouped radios, with same name Obj[2] = "9,10" //element numbers that equal the grouped radios, with same name // rember, at most, one is selected from each group var checks = new Array; var errs = new Array; var doCont = 0 var msg; for(var i=0;i<Obj.length;i++) { checks[i] = Obj[i].split(',') } for(var i=0;i<checks.length;i++) { theCon = "" for(var j=0;j<checks[i].length;j++) { theCon += document.myForm.elements[checks[i][j]].checked if(j != checks[i].length - 1) { theCon += " || " } } if(!eval(theCon)) { errs[errs.length] = document.myForm.elements[checks[i][0]].name doCont -= 1 } } msg = "There were "+ doCont * -1 +" errors in your submission in the fields:\n " for(var k=0;k<errs.length;k++) { msg += "\n" + errs[k] } if(doCont == 0) { alert("OK") return true } else { alert(msg) return false } } </script> <form name="myForm" onsubmit="return validate()"> <input type="radio" name="a">A1<BR> <!-- Element #0 --> <input type="radio" name="a">A2<BR> <!-- Element #1 --> Field: <input type="text"><BR> <!-- Element #2 --> <input type="radio" name="b">B1<BR> <!-- Element #3 --> <input type="radio" name="b">B2<BR> <!-- Element #4 --> <input type="radio" name="b">B3<BR> <!-- Element #5 --> Field: <input type="text"><BR> <!-- Element #6 --> Field: <input type="text"><BR> <!-- Element #7 --> Field: <input type="text"><BR> <!-- Element #8 --> <input type="radio" name="c">C1<BR> <!-- Element #9 --> <input type="radio" name="c">C2<BR> <!-- Element #10 --> <input type="submit"><BR> </form>All you need to do is change the form, and group the buttons in the top of the script as shown. jtf 03-06-2004, 09:12 PM Hi I am not sure that will work in this application. Although I will be using that for other things for sure. I was kind of looking for a test and a warning for each group of radio buttons. It is for a series of tests each question having a different number and they are True or False Questions. that is why I was looking for something I could just repeat the "if" for each new question <p> <input type="radio" name="question1" value="T"> True <input type="radio" name="question1" value="F"> False </p> I was looking for a way to make each question be verified on submit and the validation appear as a separate window for each question. Like this field validation script <!-- Beginning of JavaScript Code ------------- function checkdata() { var uppervalue; if (document.request.name.value.length == 0) { alert ("The 'Name' field requires an entry in this format ( John Smith )."); return false; } if (document.request.street.value.length == 0) { alert ("The 'Street' field requires an entry in this format ( 123 Maple St, Dr, Way, etc ) "); return false;. } if (document.request.city.value.length == 0) { alert ("The 'City/Town/Hamlet' field requires an entry please enter in your City/Town/Hamlet."); return false; } if (document.request.region.value.length == 0) { alert ("The 'Region' field requires an entry please select your Regional District."); return false; } if (document.request.postal_code.value.length == 0) { alert ("The 'Postal Code' field requires an entry in this format ( V2V 2V2 )"); return false; } if (document.request.phone.value.length == 0) { alert ("The 'phone' field requires an entry in this format ( 111-111-1111 )"); return false; } if (document.request.submit_by.value.length == 0) { alert ("The 'Email' field requires an entry in this format ( you@yourhost.com, net, etc )"); return false; } return confirm('Did you select your Regional District? If not press cancel and select your Regional District? If you selected your Regional District press OK. Your will be contacted within 72 hrs by our local site surveyor. If you have successfully submitted the form you it will transport you to our Thank You page.'); } // -- End of JavaScript code -------------- --> </SCRIPT> Is there no way to alter these variables to fit radio buttons. I can add and subtract as many "if's" except it does not seem to work with radio buttons I tried adding this if (document.request.mailing_list.value.cheked == 0) { alert (""); return false; I also tried the == as "()" But I am not a very good code guy, I just play around. Thanks for your reply and any other help you could give. steelersfan88 03-06-2004, 10:02 PM radio button:if(document.myForm.myRadio[i].checked) { alert("Checked!") }In this script, i represents the radio button that is checked, butou have to loop to see if one of the radios is checked with the same name:for(var i=0;i<document.myForm.myRadio.length;i++) { if(document.myForm.myRadio[i].checked) { alert("Radio button cheked \(option "+ Number(i) - Number(-1) +"\)") } }That will tell you which radio button of a group set is checked. ___________________________________________ The approach I used should work this. Just replace at the top the Obj array with the required radios. Example, if the firt two fields are group radios, Obj[0] = "0,1". Not this is NOT 1,2 (elements start as 0 because it is an array of each field). You could simply put all the elements into this array as shown and then it will validate if one is checked. Then when it is done, it will alert if any fields have been missed and tell the user which ones, and when all fields are ok, will submit. I'm not quite sure why you don't want this approach, I, in particular, like doing validation scripts, and this is one of the good ones I've written and it lonely does radio buttons, seems just what you need! (But you would know beter than me!) jtf 03-12-2004, 07:42 AM Hi Steelers quote seems just what you need! (But you would know beter than me!) End Quote Na I do not know anything. I just didn't get it. The text fields threw me off. It took me playing with it for some time(code ignorant) to get it. this is cool Thank you so much lag062 04-04-2005, 11:34 AM if(document.myForm.myRadio[i].checked) { alert("Checked!") } i am trying to create an PHP form that checks to see if a radio button as been selected before submitting information. there are two radio buttons, and the user needs to pick one. Does this line of code i quoted above do this checking, and if it does where would it go in the code. sorry im very new to this and any help would be very much appreciated. Thanks you in advanced webdeveloper.com
Copyright Internet.com Inc., All Rights Reserved. |