วันอาทิตย์ที่ 19 ธันวาคม พ.ศ. 2553

radio button validation


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />

<title>radio check demo</title>

<script type="text/javascript">
function test(f){
var t1;for(var i=0;i<f.q1_1.length;i++) if (f.q1_1[i].checked == true) t1 = true;
if (!t1) {alert('radio group 1 empty.'); f.q1_1[0].focus();return false;}

var t2;for(var i=0;i<f.q1_2.length;i++) if (f.q1_2[i].checked == true) t2 = true;
if (!t2) {alert('radio group 2 empty.'); f.q1_2[0].focus();return false;}

return true;
}
</script>
</head>


<body>

<form method="post" action="" onsubmit="return test(this);" name="f1" id="f1">
1.1 ???
<input name="q1_1" type="radio" id="rdo5" value="5" >
<input name="q1_1" type="radio" id="rdo4" value="4">
<input name="q1_1" type="radio" id="rdo3" value="3">
<input name="q1_1" type="radio"id="rdo2" value="2">
<input name="q1_1" type="radio" id="rdo1" value="1">
1.2 ???
<input type="radio" name="q1_2" id="rdo5_2" value="5">
<input type="radio" name="q1_2" id="rdo4_2" value="4">
<input type="radio" name="q1_2" id="rdo3_2" value="3">
<input type="radio" name="q1_2" id="rdo2_2" value="2">
<input type="radio" name="q1_2" id="rdo1_2" value="1">

<input type="submit" />

</form>

</body>
</html>


เช็คหลายๆ radiobox

<?php $radio_size = 3;?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-874" />

<title>radio check demo</title>

<script type="text/javascript">

var radio_size = <?php echo $radio_size;?>;
function radio_checked(rdo){
var t1=false;for(var i=0;i<rdo.length;i++) if (rdo[i].checked == true) t1 = true;
return t1;
}

function test(f){
for(var i=1;i<=radio_size;i++)
if (!radio_checked(f['q'+i])){
alert('radio group '+i+' empty.');
f['q'+i][0].focus();
return false;
}
return true;
}
</script>
</head>


<body>

<form method="post" action="" onsubmit="return test(this);" name="f1" id="f1">

<?php foreach(range(1,$radio_size) as $i):?>
<hr /><?php echo $i;?> ???
<input name="q<?php echo $i;?>" type="radio" value="5" >
<input name="q<?php echo $i;?>" type="radio" value="4">
<input name="q<?php echo $i;?>" type="radio" value="3">
<input name="q<?php echo $i;?>" type="radio" value="2">
<input name="q<?php echo $i;?>" type="radio" value="1">
<?php endforeach;?>

<input type="submit" />

</form>

</body>
</html>

ไม่มีความคิดเห็น:

แสดงความคิดเห็น