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
RickerRicker 

Cancel button on VF page

I have cancel button on VF page which displays bunch of required fields when users click cancel instead of saving the record is saved without filling of required fields I want to throw some error when user clicks cancel stating there is a required field that needs to be filled, is there any way I can accomplish this 
Suraj TripathiSuraj Tripathi
Hi MHK,

Try below code:

<apex:pageBlock id="myblock">
    Email <apex:inputText value="{!email}" id="email"/><br/><br/>
    <div class="errorMsg">
        <strong></strong>&nbsp;{!emailError}
    </div>
    <apex:commandButton value="Click me!"  action="{!checkEmail}"/>
</apex:pageBlock>

Apex:
public void checkEmail() {
    if(!Pattern.matches('[a-zA-Z0-9._-]+@[a-zA-Z]+.[a-zA-Z]{2,4}', email))
        {
         emailError ='Either first name or middle name must be defined';
        }
}

Regards,
Suraj