Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
35
rated 0 times [  38] [ 3]  / answers: 1 / hits: 41131  / 14 Years ago, sat, february 12, 2011, 12:00:00

I would like your help to develop a javascript function to validate if one of the following radiobutton group (IDType) is selected and which value is checked and view error message in (ValidationError) division in case no radio button selected ??



<td>
<div>
<span>
<input type=radio name=IDType id=IDType value=IDtype1/>
ID Type 1
<input type=radio name=IDType id=IDType value=IDtype2/>
ID Type 2
</span>
<div id=ValidationError name=ValidationError>
&nbsp;
</div>
</div>
</td>


Thanks for your help.....


More From » html

 Answers
26

First of all, as said my collegues, you cannot have the same id (IDType) for both your radio buttons.



Here is a solution with javascript only, without any jquery.



<html>
<head>
<script type=text/javascript>
function Validate() {
var radios = document.getElementsByName('IDType')

for (var i = 0; i < radios.length; i++) {
if (radios[i].checked) {
alert(Selected Value = + radios[i].value);
return true; // checked
}
};

// not checked, show error
document.getElementById('ValidationError').innerHTML = 'Error!!!';
return false;
}
</script>
</head>
<body>
<form>
<div>
<span>
<input type=radio name=IDType value=IDtype1/>
ID Type 1
<input type=radio name=IDType value=IDtype2/>
ID Type 2
</span>
<div id=ValidationError name=ValidationError>
</div>
</div>
<input type=submit value=Submit onclick=return Validate(); />
</form>
</body>
</html>

[#93767] Thursday, February 10, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
devane

Total Points: 451
Total Questions: 88
Total Answers: 100

Location: India
Member since Wed, Aug 26, 2020
4 Years ago
;