+ Start a Discussion
vanessa veronvanessa veron 

Message error Javascript

Hello I'm using JS to create error messages when a field is blank ...

This works for InputText:

if(document.getElementById("{!$Component.theform.block01.nomJobIP}").value == "" ||
document.getElementById("{!$Component.theform.block01.mailIP}").value == "" ||
document.getElementById("{!$Component.theform.block01.nomJobIP}").value == "")
{
    alert("messsage");
}

To CHECK BOX and SELECT OPTION does not work.
Why????
if(document.getElementById("{!$Component.theform.block01.ckLun}").checked == false &&
document.getElementById("{!$Component.theform.block01.ckMar}").checked == false &&
document.getElementById("{!$Component.theform.block01.ckMer}").checked == false &&
document.getElementById("{!$Component.theform.block01.ckJeu}").checked == false &&
document.getElementById("{!$Component.theform.block01.ckVen}").checked == false &&
document.getElementById("{!$Component.theform.block01.ckSam}").checked == false &&
document.getElementById("{!$Component.theform.block01.ckDim}").checked== false
){
    alert("messsage");
}

var aa = document.getElementById("{!$Component.theform.block01.selMoisFreqSem}").value;
var bb = document.getElementById("{!$Component.theform.block01.selMoisFreq}").value;

if(aa == '' && bb == '' || aa == null && bb == null)
{
    alert("select NULL");
}

Thank you!!!!
Vishant ShahVishant Shah
Hello Vanessa,

I think you might need to convert the element to a checkbox and then evaluate the checked property of the checkbox.


I could be wrong here though

Vish
Abhinav GuptaAbhinav Gupta
Your approach to access checkbox and selectlist is correct, I tried the same in very simple page, and values are coming correctly. Here is the page code:

<apex:page controller="SCTest">
    <apex:form id="theform">
    	<apex:inputCheckbox value="{!checked}" id="ckMar" />
        
        <apex:selectList value="{!country}" id="selMar">
        	<apex:selectOption itemValue="IN" itemLabel="India"></apex:selectOption>
        </apex:selectList>
    <script>
    	alert(document.getElementById("{!$Component.theform.ckMar}").checked);
    	alert(document.getElementById("{!$Component.theform.selMar}").value);
    </script>

    </apex:form>
    
    
</apex:page>

Seems you need to check your Javascript conditions, specifically this one

if(aa == '' && bb == '' || aa == null && bb == null){
    alert("select NULL");
}

It could be 
if(!aa ||  !bb){
alert(...)
}


vanessa veronvanessa veron
Hi, didn't work!!!!!
Abhinav GuptaAbhinav Gupta
I would suggest opening Javascript console in any browser and using console.log() to print out values you are getting in javascript. You can even debug it easily in any browser like firefox or chrome, just open debug console and set break point manually, OR add debugger; to your code where you want to debug for ex. 

// browser will pause here for debugging
debugger;
var aa = document.getElementById("{!$Component.theform.block01.selMoisFreqSem}").value;
var bb = document.getElementById("{!$Component.theform.block01.selMoisFreq}").value;

if(aa == '' && bb == '' || aa == null && bb == null)
{
    alert("select NULL");
}


oleksiyoleksiy
Try
((aa == '' && bb == '') || (aa == null && bb == null))
For the checkboxes you use AND, so it should be triggered only when all checkboxes are unchecked.
vanessa veronvanessa veron
Thank you, but It didn't work!

I have checkbox for each day of the week and I have too the selectList for the periods.

I like to check with JS if the checkboxes are DISABLE, an error message appears ...
I can not check if my checkbox are DISABLE and if my selectList are DISABLE.
Somebody help me!

User-added image

Thank you