1

I am not able to detect what I am doing wrong in this piece of code

<apex:page >
<apex:form id="myForm">
    <apex:pageblock id="pb1">
        <apex:pageblockSection id="pbs1">
            <apex:inputText id="Name" label="Enter Name" required="true"/>
            <apex:inputCheckbox id="checkbox1" label="Check Me" />
            <apex:commandButton value="Click Me !" onclick="getCheckBoxValue();"/>
        </apex:pageblockSection>
    </apex:pageblock>
</apex:form>
<script language="javascript">
    function getCheckBoxValue()
    {

       if(document.getElementById('{!$Component.myForm.pb1.pbs1.Name}').value == '')
        {
            alert('Name is mandatory');
        }

       else
       {
           alert('You have entered the Name');
       }
    }
</script>

The below code is working fine:

<apex:page >
<apex:form id="myForm">
    <apex:pageblock id="pb1">
        <apex:pageblockSection id="pbs1">
            <apex:inputCheckbox id="checkbox1" label="Check Me" />
            <apex:commandButton value="Click Me !" onclick="getCheckBoxValue();"/>
        </apex:pageblockSection>
    </apex:pageblock>
</apex:form>
<script language="javascript">
    function getCheckBoxValue()
    {
       var status = (document.getElementById('{!$Component.myForm.pb1.pbs1.checkbox1}').checked);
       if(status==true)
       {
           alert('You have checked it.');
       }
       else
       {
           alert('Check it first.');
       }
    }
</script>

Alerts Coming for Checkbox field

Alerts Coming for Checkbox field

3
  • alerts coming or not? Commented May 19, 2016 at 7:44
  • Alerts are not coming when I am using it for Input Text Field,though they are coming for Check box field Commented May 19, 2016 at 7:45
  • actually you do not need any alert or anything extra as you already added "required". whenever u submit form will automatically alert you for that field. Commented May 19, 2016 at 7:51

2 Answers 2

2

I believe you can achieve it by required attribute alone in inputText instead of using Javascript. You have to include:

<apex:pageMessages/>

in you VF page to show any errors.

Error:

<apex:page >
    <apex:pagemessages />
    <apex:form id="myForm">
        <apex:pageblock id="pb1">
            <apex:pageblockSection id="pbs1">
                    <apex:inputText id="Name" label="Enter Name" required="true"/>

                <apex:inputCheckbox id="checkbox1" label="Check Me" />
                <apex:commandButton value="Click Me !" onclick="getCheckBoxValue();"/>
            </apex:pageblockSection>
        </apex:pageblock>
    </apex:form>
    <script>
    function getCheckBoxValue()
    {
        console.log('Getting Val. ');
        if(document.getElementById('{!$Component.myForm.pb1.pbs1.Name}').value == '')
        {
            alert('Name is mandatory');
        }

        else
        {
            alert('You have entered the Name');
        }
    }
    </script>
</apex:page>
2
  • I just dont know when I did this , why the alerts for checkboxes stopped to come? Commented May 19, 2016 at 8:16
  • @ShobhitSaxena The above code is working fine for me. Please check for any JavaScript errors in browsers console.
    – manjit5190
    Commented May 19, 2016 at 8:55
1
<apex:page >
<apex:pageMessages />
<apex:form id="myForm">
    <apex:pageblock id="pb1">
        <apex:pageblockSection id="pbs1">
            <apex:inputText id="Name" label="Enter Name" />
            <apex:inputCheckbox id="checkbox1" label="Check Me" />
            <apex:commandButton value="Click Me !" onclick="getCheckBoxValue();" rendered="true"/>
        </apex:pageblockSection>
    </apex:pageblock>
</apex:form>
<script language="javascript">
function getCheckBoxValue()

{
    //console.log('Getting Val. ');

   var status = (document.getElementById('{!$Component.myForm.pb1.pbs1.checkbox1}').checked);
   var name=(document.getElementById('{!$Component.myForm.pb1.pbs1.Name}').value);
   if((name == '') && (status==false) )
    {

                alert('Enter Name First');
                alert('Check it');

    }



    else 
    if(( name == '') && (status==true) )
    {
                alert('Enter Name First');

                alert('Checked');

    }

    else 
    if(( name != '') && (status==true) )
    {

                alert('Name Entered');
                alert('Checked');

    }
     else 
    if(( name != '') && (status==false) )
    {

                alert('Name Entered');
                alert('Check it');

    }

         }

</script>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .