PDA

View Full Version : Unstoppable Form Validation



devil_vin
09-24-2007, 03:10 PM
Hi..guys! I have a function here to validate null selection of drop down menu and radio button.However,it seems like alert box couldn't be stopped display even though both fileds were already selected.Thanks for your generious help



<script language="JavaScript" type="text/JavaScript">
function nullValidation() {
var x=document.getElementById("movieList");
for (var i=0;i<x.length;i++)
{
if(x.elements[i].checked=="" || x.elements[i].selectedIndex == 0)
{
alert("Please select "+x.elements[i].title+".");
x.elements[i].focus();
return false;
}

}
return true;
}
</script>




<form action="" method="post" name="movieList"
onSubmit="return nullValidation();">

devil_vin
09-24-2007, 05:11 PM
Problem resolved.



<form action="" method="post" name="movieList"
onSubmit="return validate(this);">

<script language="JavaScript" type="text/JavaScript">
function validate(which) {
var selects = which.getElementsByTagName('select');
var radios = which.getElementsByTagName('input');
for(sel = 0; sel < selects.length; sel++) {
if(selects[sel].options[selects[sel].selectedIndex].text == 'Select') {
alert('error [select]');
return false;
}
}

var rselCount = 0;
for(radio = 0; radio < radios.length; radio++) {
if(radios[radio].checked) {
rselCount++;
}
}

if(rselCount == 0) {
alert('error [radio]');
return false;
}
return true;
}

</script>