dawgmom Posted June 2, 2008 Share Posted June 2, 2008 Hi. I'm no pro at JS, but most times can muck my way though tweaking exsisting code - except for this. I need to validate [as in none selected] a set of radio buttons on a form: <form name="form2" method="post" action="processform.php" onSubmit="return validate.check()"> <input type="radio" name="radio" id="yes" value="pickup" onBlur="trim('pickup')" /> Yes.</td> <td width="55%"><input type="radio" name="radio" id="usps" value="no" onBlur="trim('usps')" /> No.</td> This is the JS Validation Code I found online and that I'm using but it only validates text fields, not radio buttons: function $$(id) { try { var tmp = document.getElementById(id).value; } catch(e) { alert("Field " + id + " does not exist!\nvalidation is configured on a field with no ID"); return false; } if(tmp == "") { alert("Field " + id + " cannot be empty"); return false; } return tmp; } var required = { field : [], add : function(name, type) { this.field[this.field.length] = [name,type]; }, out : function() { return this.field; } } var validate = { check : function() { var tmp; // loop all required fields for(var i=0; i<required.field.length; i++) { // check the form field exists this.tmp = $$(required.field[i][0]); if(this.tmp) { if(this.checkit(required.field[i][0],required.field[i][1])) { // validated okay } else { alert("Field "+required.field[i][0]+" not valid\n"); document.getElementById(required.field[i][0]).focus(); return false; } } else { try { document.getElementById(required.field[i][0]).focus(); } catch(e) { } return false; } } // for return true; }, checkit : function(value,type) { exp : ''; switch(type) { case "NOT_EMPTY": if(this.trim($$(value)).length < 1) { return false; } else { return true; } break; case "ALPHA": exp = /^[A-Za-z]+$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "ALPHASPACE": exp = /^[A-Za-z ]+$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "NUMERIC": exp = /^[0-9]+$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "NUMERICPLUS": exp = /(^-*\d+$)|(^-*\d+\.\d+$)/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "ALPHANUM": exp = /^[a-zA-Z0-9]+$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "ALPHANUMSPACE": exp = /^[a-zA-Z0-9 ]+$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "EMAIL": exp = /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "YYYYMMDD": exp = /^(19|20)[0-9][0-9][- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "DDMMYYYY": exp = /^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)[0-9][0-9]$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; case "MMDDYYYY": exp = /^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)[0-9][0-9]$/; if($$(value).match(exp)==null) { return false; } else { return true; } break; default: exp = new RegExp(type); if($$(value).match(exp)==null) { return false; } else { return true; } } // switch }, trim : function(s) { return s.replace(/^\s+/, '').replace(/\s+$/, ''); } } function $val(id) { return document.getElementById(id); } function trim(id) { $val(id).value = $val(id).value.replace(/^\s+/, '').replace(/\s+$/, ''); } Can someone please, please, please tell me what and where to add the validation for radio buttons? Should my form include 'onBlur' for radio buttons? BTW, I'm using PHP to process the form. Thanks so much. Quote Link to comment Share on other sites More sharing options...
hansford Posted June 6, 2008 Share Posted June 6, 2008 I can help you with doing it the javascript way or php. the php way must be submitted as it needs to be in a $_POST var only those radios that were checked will be in the $_POST. The javscript way is a pain-I mean real pain. You must iterate through the entire form skipping anything thats not a radio button. checking if the radios checked and then keep track of all the other radios that have the same name that weren't checked. Heres an example that I did for another person in javascript. It checks a few other things, but the point is still the same - to get all the values of the selected radios. At the end of the script the answers are stroed in var str. this gets passed to a hidden element in the form to php in the $_POST which explodes("&", $str) to get the results in an array. --------------------------------- <form name='examform' method='post' action='index.php'> <input type='hidden' name='page' value='2'/> <input type='hidden' name='answer' value='hh'/> 1.)which is not a plant?<br><br>Tree<input type='radio' name='1' value='tree'/> <br>Spore<input type='radio' name='1' value='spore'/><br> Poppy<input type='radio' name='1' value='poppy'/><br><br> 2.)how many centimeters in an inch?<br><br> 2.0<input type='radio' name='2' value='2.0'/> <br>3.35<input type='radio' name='2' value='3.35'/><br> 2.56<input type='radio' name='2' value='2.56'/><br><br> 3.)which is not a name from the bible?<br><br> Larry<input type='radio' name='3' value='larry'/> <br>Joseph<input type='radio' name='3' value='joseph'/><br> Mary<input type='radio' name='3' value='mary'/><br> David<input type='radio' name='3' value='david'/><br><br> <input type='button' value='submit answers' name="action" onclick='validate(this.form)'> </form> <script language='javascript'> function validate(frm){ var answers = new Array(); var count = 0; for(var i=2; i < frm.length - 1;i++){ var missedarray = new Array(); var count2 = 0; var missedname = new Array(); var missed = 0; var question = frm.name; while(frm.name == question){ if(frm.checked){ missedarray[count2++] = 0; answers[count++] = frm.value; } else{ missedarray[count2++] = 1; } i++; } for(m = 0; m < missedarray.length; m++){ if(missedarray[m] == 0){ missed = 0; break; } else{ missed = 1; continue; } } if(missed){ var str = ""; alert('A question was missed. Please review your answers'); return; } i += - 1; } var str = ""; for(var i=0; i < answers.length;i++){ str += answers + "&"; } frm[1].value = str; frm.submit(); } </script> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.