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
ChinnoyChinnoy 

How to add a not null validation in visual force page?

Hi,

i have created a field called "Name" and now i need to add a not null validation to that field.

if i created a custom button called "save", if i click save button before inserting data to the Name field i need to get a validation message.

can anyone help me on this?

Best Answer chosen by Chinnoy
sandhya reddy 10sandhya reddy 10
Hi Venkat Gantasala,

You can use apex Messages in your controller 
if(name==' ')
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Here is some important information for you, Mr User!  So please read it!');
ApexPages.addMessage(msg);
}
Please let us know if this helps you

Thanks and Regards
sandhya
 

All Answers

Jerun JoseJerun Jose
Simplest way would be to add the required attribute on the name field in your visualforce page.
ChinnoyChinnoy

HI jerun..

i have added required attribute to that field but my question is if he fails to give value how to throw an error in the visual force page.?

Jerun JoseJerun Jose
Hi Venkat,

You can use the apex:pagemessages or apex:messages tag. These will automatically show error messages for form level validations
Ajay K DubediAjay K Dubedi
Hi Venkat Gantasala,
You can also use Javascript for form validation and to show error messages. The following VF page will show an error message when Name field is null.
<apex:page standardController="Account">
<apex:form id="createAccount">
<h1>New Account</h1><br/><br/>
<apex:pageblock title="Add Account" id="thepageblock">
<apex:pageblockSection id="thesection" >
<apex:inputField value="{!Account.Name}" required="true" id="accountName"/>
<apex:inputField value="{!Account.Phone}"/>
</apex:pageblockSection>
<apex:pageBlockButtons Location="Bottom" title="click to save">
<apex:commandButton action="{!save}" value="Save" onclick="confirmation()"/>
</apex:pageBlockButtons>
</apex:pageblock>
</apex:form>
<script type="text/javascript">
function confirmation(){
var x = document.getElementById('{!$Component.createAccount.thepageblock.thesection.accountName}').value;
if(x==""){
alert("Please enter your Name in Account Name field");
}
else{
alert("Do you really want to insert the new record with Name : "+x);
}
}
</script>
</apex:page>

Regards,
Ajay
sandhya reddy 10sandhya reddy 10
Hi Venkat Gantasala,

You can use apex Messages in your controller 
if(name==' ')
{
ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Here is some important information for you, Mr User!  So please read it!');
ApexPages.addMessage(msg);
}
Please let us know if this helps you

Thanks and Regards
sandhya
 
This was selected as the best answer
Jerun JoseJerun Jose
Wanted to pitch in my thoughts in this discussion.

@Ajay - using javascript alert messages are not recommended as an industry trend. These are very troublesome when accessing from mobile browsers. Also, Salesforce does have standard components that can support this, so no need to go for a custom JS solution.

@Sandhya - Yes, in case we need to throw a custom message we can use a controller/extension and build the error string that needs to be returned. This returned string will be displayed in the apex:messages element on the visualforce page.