PDA

View Full Version : Multiple groups of radio buttons validation



mkronix
04-29-2010, 12:52 PM
Hi guys,

I have a form with input text, dropdown menu and groups of radio button. The validation for input text and dropdown menu works fine with these js validator :


<script language="JavaScript">
function formValidator(){

// Make quick references to our fields

var pensyarah = document.getElementById('pensyarah');
var kksem = document.getElementById('kksem');



// Check each input in the order that it appears in the form!
if(madeSelection(pensyarah, "Sila pilih Pensyarah")){
if(madeSelection(kksem, "Sila pilih Kod kursus")){



return true;
}
}


return false;

}



function isEmpty(elem, helperMsg){
if(elem.value.length == 0){
alert(helperMsg);
elem.focus(); // set the focus to this input
return true;
}
return false;
}

function isNumeric(elem, helperMsg){
var numericExpression = /^[0-9\s\:]+$/;
if(elem.value.match(numericExpression)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphabet(elem, helperMsg){
var alphaExp = /^[a-zA-Z\s\.]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function isAlphanumeric(elem, helperMsg){
var alphaExp = /^[0-9a-zA-Z\s]+$/;
if(elem.value.match(alphaExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}

function madeSelection(elem, helperMsg){
if(elem.value == "*Tiada Pilihan"){
alert(helperMsg);
elem.focus();
return false;
}else{
return true;
}
}


function emailValidator(elem, helperMsg){
var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
if(elem.value.match(emailExp)){
return true;
}else{
alert(helperMsg);
elem.focus();
return false;
}
}


</script>

the problem is, how to add the groups of radio button validation script inside the existing js validator.. is it posible to merge it in one function? I try to add this code but the function its not working at all...


var flag1 = 1;

if (document.thisform.qs1.type == "textarea")
{
if (document.thisform.qs1.value)
{
flag1 = 0;
}
}
else
{
for (var i = 0; i < document.thisform.qs1.length; ++i)
{
if (document.thisform.qs1[i].checked)
{
flag1 = 0;
}
}
}
if (flag1)
{
alert('Please answer Question # 1.');
if (document.thisform.qs1.type == "textarea")
{
document.thisform.qs1.select();
}
return false;
}
var flag2 = 1;
if (document.thisform.qs2.type == "textarea")
{
if (document.thisform.qs2.value)
{
flag2 = 0;
}
}
else
{
for (var i = 0; i < document.thisform.qs2.length; ++i)
{
if (document.thisform.qs2[i].checked)
{
flag2 = 0;
}
}
}
if (flag2)
{
alert('Please answer Question # 2.');
if (document.thisform.qs2.type == "textarea")
{
document.thisform.qs2.select();
}
return false;
}
var flag3 = 1;
if (document.thisform.qs3.type == "textarea")
{
if (document.thisform.qs3.value)
{
flag3 = 0;
}
}
else
{
for (var i = 0; i < document.thisform.qs3.length; ++i)
{
if (document.thisform.qs3[i].checked)
{
flag3 = 0;
}
}
}
if (flag3)
{
alert('Please answer Question # 3.');
if (document.thisform.qs3.type == "textarea")
{
document.thisform.qs3.select();
}
return false;
}
} .........

here is the full code I am using (the code is too long to put in here)
http://www.modernessence.net/e1flashz/images_tmp/form_code.txt

this is how the form looks like ..
http://modernessence.net/e1flashz/images_tmp/form_validation.jpg

Any ideas how to modified it ..?

Thank you in advance.

(sorry for my english, i'm from malaysia)