/    Sign up×
Community /Pin to ProfileBookmark

Validation code problem

I have a piece of code that validates whether one button has been selected from a bunch of dynamically created radio buttons.

[code]
//Validation Code

var checked = false;
var Sizes = document.BuyProduct.Sizes;

for (var i=0; i<Sizes.length; i++)
{
if (Sizes[i].checked) {
checked = true;
}
}

if(!checked)
{
Errors++
document.getElementById(‘mySpan1a’).style.display=’block’;
document.getElementById(‘mySpan1a’).innerHTML=’Please Select a Size!’;
document.getElementById(‘mySpan1b’).style.display=’inline’;
document.getElementById(‘mySpan1b’).innerHTML=’! ‘;
}
else
{
document.getElementById(‘mySpan1a’).style.display=’none’;
document.getElementById(‘mySpan1a’).innerHTML=”;
document.getElementById(‘mySpan1b’).style.display=’none’;
document.getElementById(‘mySpan1b’).innerHTML=”;
}

// Form code

<?php echo $Err1?><span class=”mySpan1b” id=”mySpan1b”></span><span class=”head4″>Available Sizes:</span><span class=”errmsg”> *</span>
<table style=”position:relative;”><tr>

<?php

$counter=0;
$X=0;
$i=0;
$IsEmpty1 == false;

while ($IsEmpty1 == false)
{
if($AvailableSizes[$X] != “”)
{
$id1 = “Size”.$i;
if($counter%6==0&&$counter!=0) echo “</tr><tr>”;
echo”<td>$AvailableSizes[$X]</td>”;
echo “<td><label><input type=’radio’ id=’$id1′ name=’Sizes’ value=’$AvailableSizes[$X]'”;
if($AvailableSizes[$X] == $SizeSel){echo “Checked”;} echo”></label></td></tr><tr>”;
++$counter;
}
else
{
$IsEmpty1 = true;
}
$i++;
$X++;
}
?>
</tr></table>
[/code]

The validation code works, but to a point. This code assumes that there will be more than 1 radio button to choose from and becomes an array, however occassionally there will be only a single button which doesn’t turn the selection into an array, and thus stops the validation code from working and displaying the error message for that option.

So what I need is a modified version of the validation that can account for when the selection is an array and when it is only a single option as well.

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@FangApr 23.2008 — var Sizes = document.getElementsByName('Sizes');
×

Success!

Help @psychofox19 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...