Validating A Single Radio Button Is Not Working In Available Javascript Validation Script
Solution 1:
Always (!) use the var keyword. Otherwise your variables will be in the global scope (yes, even those in function bodies), which can make for some bugs that are hard to track down.
As @Felix pointed out, creatorusers
will only be an array if there is more than one element with that name in the form. You can create a single-element array when necessary to work around that.
Here is an abstracted function that can validate an arbitrary checkbox list.
function ensureChecked(checkboxes, error) {
if (checkboxes) {
var cbx = (checkboxes.length > 0) ? checkboxes : [checkboxes];
for (var i=0; i<cbx.length; i++) {
if (cbx[i].checked) {
returntrue;
}
}
alert(error);
}
returnfalse;
}
call as
ensureChecked(frm.creatorusers, "You must select a Creator User!");
Solution 2:
Ah now I got. If you only have one radio button, then frm.creatorusers
is not an array. Just skip it:
var mycreator = -1;
var checked = false;
if(typeof frm.creatorusers.length === 'number') {
for (var i=frm.creatorusers.length; i--; ) {
if (frm.creatorusers[i].checked) {
mycreator = i;
checked = true;
break;
}
}
}
elseif(frm.creatorusers.checked){
mycreator = //? what here?
checked = true;
}
if(!checked) {
alert("You must select a Creator User!");
returnfalse;
}
If mycreator
was just for checking whether a button was selected or not, you can completely remove it from the code above.
Some further notes to your code:
- Always declare variables with
var
, otherwise they will be global. - Use
break
to end a loop. - Maybe it is just because of copy and paste, but having a lot of radio buttons with the same value does not make much sense.
Solution 3:
You can do something like this:
functionvalidate(frm){
var isChecked = false;
for (var i=0; i<frm.elements.length; i++)
{
if (frm.elements[i].type === 'radio'){
if (frm.elements[i].checked === true){
isChecked = true;
break;
}
}
}
if (isChecked === true){
returntrue;
}
else{
alert('You should select an option first !');
}
}
Now you should call above function on onsubmit
event of the form:
<formonsubmit="return validate(this);">
Now the validate
function will make sure that at least one radio button is checked otherwise it won't submit.
Solution 4:
this should do it
functionisRadioSelected(btn) {
if(typeof btn.length === 'number') {
for(var i=0;i<btn.length;i++)
if(btn[i].checked) returntrue
}else{
if(btn.checked) returntrue
}
returnfalse
}
Solution 5:
You could try something like this instead:
<html><head><scripttype="text/javascript">functionconfirmsubmit() {
var btn = document.formname.buttonnameif (btn.checked == false)
{
window.alert("You did not click the button.");
btn.focus();
returnfalse;
}
returntrue;
}
</script></head><body><formmethod="post"action="mailto:youremail@yourdomain.com"name="formname"onsubmit="return confirmsubmit();">
click here: <inputtype="radio"name="buttonname"><br /><p><inputtype="submit"value="Submit"name="submit"></p></form></body></html>
Post a Comment for "Validating A Single Radio Button Is Not Working In Available Javascript Validation Script"