Java Script Validation on a radio button

Hi guys,

I have a radio button on a form with two values - a & b. I also have a button on the same form. I want this button to validate the radio button if none are checked and then submit. This my code:

//Form validation

var validString = '';

var validError = false;

var form = document.forms[0];



var radios	= form.radios.checked;

if (radios == ‘’)

	{

		validString = validString + 'Please check\n';

		validError = true;

	}

if (validError == true)

	{

		alert('The following information is required:  \n\n'+validString);

	}

		

else 

	{

		document.forms[0].status.value = "2"

		document.forms[0].SaveRef.value="FirstApprove"

		document.forms(0).display.value = "noedit";

		document.forms[0].submit();

		alert('Your Form Has Been Successfully Submitted ' );  	

   	}

Subject: Java Script Validation on a radio button

Hello,

Whatever i understood from your question is, You want to validate the Radio buttion.

You can try with for loop and checking radion box propety .Checked ==True

if then you can submit your page else you cannot…

if i m wroing then please submit your question with broad explanation.

Regards,

Dev

Subject: Java Script Validation on a radio button

You have to check every possible value in radios to see if it is checked or not.Best is to create a standard-function for checking radiobuttons you can use anywhere in your code

Your code would be:

var radios = form.radios;

if (checkRadioButton(radios) == false) validString = validString + ‘Please check\n’;

And your function would be:

function checkRadioButton(fieldObject){

if (!fieldObject[0]) { //only one option is available

		if (fieldObject.checked ) {

			return true;

		}

} else {				

        var length=fieldObject.length;

	for (var count=0;count<length;count++){

		if (fieldObject[count].checked) {

			return true;

		}

	}

}	

return false;

}

Subject: RE: Java Script Validation on a radio button

Hi Rene,

Thank you for your post. Your code works perfectly.

Viraj

Subject: RE: Java Script Validation on a radio button

Can i go further?

If my radio button has two values:

approve

reject

If the user selects approve and presses the reject button show can i get the reject button to inform that the approve button is selected and they have chosen to reject.

Subject: RE: Java Script Validation on a radio button

Why would you want to have a radio button for approve/reject AND buttons for approve/reject at the same time? That’s confusing to the user, at least. And it will not get better by adding further validation logic.

All you need is one submit button and a validation to make sure, that either radio button has been clicked.

Subject: RE: Java Script Validation on a radio button

Hi,

I understand what you are saying that that makes better sense to me too. However, this is what the specification requires.

So… If the approve radio button is checked and the user clicks on reject and error should appear notifying them that they have checked approved and tried to reject.

Subject: RE: Java Script Validation on a radio button

Greetings to the guy who wrote the specs, he’s a certified idiot.

Once you’ve got the checked input (as per Rene’s script), you can retrieve its value in a similar fashion you would do for an input of type text.

radioValue = fieldObject[count].value;

When using the code in the reject button, return false, if this value does not equal “reject” (or whatever the actual value is). If you are using synonyms when defining the choices for your radio button, like

approve | 1

reject | 0

the .value property will return what is stored to the document, not what is displayed on screen (because that’s what’s really in the value attribute of the input of type radio).

Subject: RE: Java Script Validation on a radio button

“Greetings to the guy who wrote the specs, he’s a certified idiot.”

Harkpabst… very mature.

Subject: RE: Java Script Validation on a radio button

Very true in the first place …