+ Start a Discussion
sagar sanusagar sanu 

validation message using javascript

In my Vf page i have a selection radio option.. i want to have a javascript function which will show validation message whenever i click save button without selecting the radio options.. kindly plz help me with this.

   <apex:selectradio id="modeFamilyTraveling" value="{!modeFamilyTraveling}" layout="pageDirection" style="font-weight:0px !important;" >
 <apex:selectOptions value="{!transportMode}"></apex:selectOptions>
 </apex:selectradio>

Best Answer chosen by sagar sanu
Somya TiwariSomya Tiwari
Hello,

Please have a look at the following Javascript which is quite easy to change according to your need.
< script language = "javascript" >
	function validate() {
		try {
			var field1 = document.getElementsByName('{!$Component.id}');
			var selected = false;
			for (i = 0; i < field1.length; i++) {
				if (field1[i].checked) {
					selected = true;
				}
			}
			if (!selected) {
				alert("Please make a selection and enter a value into field2.");
				return false;
			}
			return true;
		} catch (e) {
			alert(e);
			return false;
		}
	} 
</script>
Please Mark the Answer as best answer if it helped you in your use case.

All Answers

Somya TiwariSomya Tiwari
Hello,

Please have a look at the following Javascript which is quite easy to change according to your need.
< script language = "javascript" >
	function validate() {
		try {
			var field1 = document.getElementsByName('{!$Component.id}');
			var selected = false;
			for (i = 0; i < field1.length; i++) {
				if (field1[i].checked) {
					selected = true;
				}
			}
			if (!selected) {
				alert("Please make a selection and enter a value into field2.");
				return false;
			}
			return true;
		} catch (e) {
			alert(e);
			return false;
		}
	} 
</script>
Please Mark the Answer as best answer if it helped you in your use case.
This was selected as the best answer
sagar sanusagar sanu
Thanks!