arwvisions Posted October 25, 2008 Share Posted October 25, 2008 I am using free contact form and want to validate whether a radio button is checked in a group. Here is the validation.js. Any suggestions? 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+$/, ''); Quote Link to comment https://forums.phpfreaks.com/topic/130099-radio-button-validation/ Share on other sites More sharing options...
Psycho Posted October 25, 2008 Share Posted October 25, 2008 I really don't see a reason for validating a radio group - just set one value as selected by default and the user can only select a different value - he cannot unselect all the values. There should be server-side validation as well. But if you really want/need to do it client side here is some sample code. I'm not going to try to interpret what you are doing above. You can just implement this as you need it: <html> <head> <script type="text/javascript"> function validate(formObj) { radioGrp = formObj.color; for(i=0; i<radioGrp.length; i++) { if (radioGrp[i].checked) { return true; } } return false; } </script> </head> <body> <form name="theform" onsubmit="return validate(this);"> What is your favorite color:<br> <input type="radio" name="color" value="blue">Blue<br> <input type="radio" name="color" value="red">Red<br> <input type="radio" name="color" value="green">Green<br> <input type="radio" name="color" value="yellow">Yellow<br> <input type="radio" name="color" value="purplpe">Purple<br> <button type="submit">Submit</button> <form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/130099-radio-button-validation/#findComment-674589 Share on other sites More sharing options...
arwvisions Posted October 25, 2008 Author Share Posted October 25, 2008 Ok I have a form that asks the question are you a US citizen. The client wants yes or no and wants is mandatory. The form Im using already has an onsubmit call. Can I put more than one onsubmit call on a form? Quote Link to comment https://forums.phpfreaks.com/topic/130099-radio-button-validation/#findComment-674605 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.