...

View Full Version : form validate , radio buttons



minusp
06-18-2010, 07:11 PM
Hello

in "validateField" function radio is not exists and I don't know how can I add a validation rule for radio buttons.
please help me .

my code is attached.

thank you

Sciliano
06-19-2010, 02:40 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>None</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript">

var labelIndex = 0;

function validate(radioGroupLabel,radioGroupName){

for (each in radioGroupName)
{
var nRadioGroup = document.getElementsByName(each);
for (i=0; i<nRadioGroup.length; i++)
{
if (nRadioGroup[i].checked)
{
radioGroupName[each] = 1;
}
}
}
radioGroupLabel[labelIndex].style.backgroundColor = "white";
labelIndex = 0;
for (each in radioGroupName)
{
if (radioGroupName[each] == 0)
{
alert('You must choose from ' + radioGroupLabel[labelIndex].firstChild.data);
radioGroupLabel[labelIndex].style.backgroundColor = "#ff6347";
return false;
}
labelIndex++;
}
alert('Thank you');
return true;
}

function init(){

var nForm = document.forms[0];
var radioGroupLabel = [];
var radioGroupName = {};
var nLabel = nForm.getElementsByTagName('label');
for (i=0; i<nLabel.length; i++)
{
if (nLabel[i].className == "radio_group")
{
radioGroupLabel[radioGroupLabel.length] = nLabel[i];
}
}
var nInput = nForm.getElementsByTagName('input');
for (i=0; i<nInput.length; i++)
{
if (nInput[i].type == "radio")
{
radioGroupName[nInput[i].name] = 0;
}
}
nForm.onsubmit = function()
{
return validate(radioGroupLabel,radioGroupName);
}
}

navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);

</script>
<style type="text/css">

label {font-family: times; font-size: 12pt;}
.radio_group {padding-left: 5px; padding-right: 25px;}

</style>
</head>
<body>
<form action="" method="post">

<label class="radio_group">Group 1:</label>
<label>AR: <input type="radio" name="radSafe1" value="AR"></label>
<label>Safe: <input type="radio" name="radSafe1" value="Safe"></label>
<label>No: <input type="radio" name="radSafe1" value="NO"></label>
<br>
<label class="radio_group">Group 2:</label>
<label>AR: <input type="radio" name="radSafe2" value="AR"></label>
<label>Safe: <input type="radio" name="radSafe2" value="Safe"></label>
<label>No: <input type="radio" name="radSafe2" value="NO"></label>
<br>
<label class="radio_group">Group 3:</label>
<label>AR: <input type="radio" name="radSafe3" value="AR"></label>
<label>Safe: <input type="radio" name="radSafe3" value="Safe"></label>
<label>No: <input type="radio" name="radSafe3" value="NO"></label>
<br><br>
<input type="submit" value="Submit">

</form>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum