Field validations on web using javascript

hiall

i m facing small problem in validating some fields based on

radio button…

proble is

my form is leave reuest form…

in that

i have one radiobutton called Hday

Hday

values: Yes,no

if i select yes

Ftime and TTime fields should get validated

if Hday is No

From and to Fields should get validated

when i select No error is appearing

like ftime valiue is null or not object…

here i m pasting my code can any modify this code to correct

var checked = false;

var buttons = document.forms[0].Type;

for (var i=0; i<buttons.length; i++)

{

if (buttons[i].checked)

{

 checked = true; 

     break;  

}   

}

if(!checked){

alert(“you have to choose a Type of Leave”);

return checked ; 

}

var checked1 = false;

var buttons1 = document.forms[0].HDay;

for (var i=0; i<buttons1.length; i++)

{

if (buttons1[i].checked)

{

alert(buttons1[i].value);

// checked1 = true;

      if (form.HDay.value=='Yes')

           alert("correct");

                 {    

if (document.forms[0].FTime.value== “”)

{

alert(“Select from time”);

return false;

}

if (document.forms[0].TTime.value== “”)

{

alert(“Select to time”);

return false;

}

checked1 = true;

break;

}

if (form.HDay.value==‘No’)

           alert("correct");

                 {    

if (document.forms[0].From.value== “”)

{

alert(“Select from Date”);

return false;

}

if (document.forms[0].To.value== “”)

{

alert(“Select to Date”);

return false;

}

   checked1 = true; 

break;

}

}

}

if(!checked1)

{

alert(" choose a Type of Leave");

return checked1 ; 

}

Subject: field validations on web using javascript…

Im not a 100% sure if i understood your code but if Hday is a radio button, that reference to Hday.value shouldn’t work. To my understanding radio buttons don’t have a ‘value’ property. Anyway since you simple want to check a radio button with a simple yes or no option, i would just validate the answer using the indexes directly, where if you entered ‘Yes’ first it would be index 0 and ‘No’ would be 1:

myRadioField = document.forms[0].radiofieldname;

fieldTovalidate = document.forms[0].fieldTovalidatename;

if(myRadioField[0].checked) //answer is yes

if (fieldTovalidate.value == ‘’) return false;

else

if (myRadioField[1].checked) //answer is No

   .....

Subject: RE: field validations on web using javascript…

Radio buttons certainly do have a value property – but document.forms[i].FieldName will return an ARRAY of radio buttons, not a single object, and the array doesn’t have a .value property. When you loop through the array to check the checked status of each of the elements, you can get the .value of the checked element and assign it to a variable declared outside of the loop for later use.

Or you can use this function I posted a while back. The function will return an array of values from any field type – handy for those times when a field may be one type when the document is in one state and another type under different circumstances (like a radio button one time and a hidden type another time). To get a string from the array, simple use the join() function.