function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ramssf70Ramssf70 

display error messages using java script in visuval force page?

Hi Folks,
can any one send  display error messages using java script ?
Devanshu soodDevanshu sood
You can use alert to display error message .
Devanshu soodDevanshu sood
if (document.myform.username.value == "") {
     document.getElementById('errors').innerHTML="*Please enter a username*";
     return false;
}

Or you can try this.
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Ram,
<apex:page standardController="Account">
<script type="text/javascript">
function test()
{

var accname=document.getElementById("{!$Component.f.pb1.pbs1.a}").value;
alert(accname);
var message=document.getElementById("{!$Component.f.pb1.msg}").textContent;
alert(message);

}

</script>

<apex:form id="f">
<apex:pageBlock id="pb1">
<apex:pageMessages id="msg" />
<apex:pageBlockSection id="pbs1">
<apex:inputField value="{!account.name}" id="a" />
<apex:inputField value="{!account.industry}"/>
<apex:commandButton action="{!save}" value="Save!"/>
<apex:commandButton onclick="test();" value="test"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

Please refer the below link for reference. Hope it will be helpful.

Please mark it as Best Answer if the information is informative.

Thanks
Rahul Kumar
karthikeyan perumalkarthikeyan perumal
Hello, 

Use below Code. 
 
<apex:page sidebar="false" >
<apex:includeScript value="/soap/ajax/35.0/connection.js" />

      <apex:form id="frm">            
      <apex:commandbutton value="Click" onclick="javascriptfunction()"/>
      </apex:form>
      
      <script>
          function javascriptfunction(){
                 
                  alert('ErrorMessage');
          }
      </script>
</apex:page>

Hope this will help you
Thanks
karthik
 
Shruti SShruti S
Try out the below code - 

Visualforce
<apex:inputField id="inptFldAge" type="text" onchange="checkAge( this )" />

JavaScript
<script type="text/javascript">
    var checkAge = function( ele ) {
        var age = parseInt( ele.value );

        if( age < 18 ) {
            window.alert( "Not Eligible for Voting" );
        }
    };
</script>
Please feel free to ask if you have any more doubts.