• Ashok.Force
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 5
    Replies

Hi,

   I have  the following Apex Page where the form shouldn't submit if there is any validation error.

 

<apex:page showHeader="false" sidebar="false" standardController="Lead"
    extensions="Beneficiary">


    <h1>NGO membership form</h1>

    <apex:form id="myform" styleClass="standard-form"
        enctype="multipart/form-data">

        <apex:outputText value="Organisation" />
        <apex:inputText value="{!Lead.Company}" label="Organisation"
            required="true" id="organisation" />
        <br></br>
        <apex:outputText value="Email" />
        <apex:inputText value="{!Lead.Email__c}" label="Email" required="true"
            id="email" />
        <br></br>
        <apex:outputText value="LastName" />
        <apex:inputText value="{!Lead.LastName}" label="LastName"
            required="true" id="lastname" />
        <br></br>

        <apex:commandButton value="Submit" onclick="validate()" immediate="false"/>
    </apex:form>


    <script>
function validate()
{

    var a = document.getElementById('{!$Component.myform.lastname}').value;
    if(a == null || a.length == 0)
    {
         alert("Last name is a Mandatory Field");
         return null;
    }

}
</script>

</apex:page>

 

If the lastname is blank. I'm getting the validation error. But the form still submits. How to stop the form submission if there is any validation error?