This is the code that will work with addError methdod
account.NumberOfEmployees.addError('You must enter a value.'); this way you can add error message on field level ..
Page
<apex:page controller="AddErrorCtrl" tabStyle="Account">
<apex:messages />
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!" id="pblock">
This is your new page for the {!name} controller. <br/>
You are viewing the {!account.name} account.
<p>Number of Locations: <apex:inputField value="{!account.NumberofLocations__c}"
id="Location_validation"/>
(Enter an alphabetic character here, then click save to see what happens.) </p>
<p>Number of Employees: <apex:inputField value="{!account.NumberOfEmployees}"
id="Employee_validation"/>
(Enter an alphabetic character here, then click save to see what happens.) </p>
<p />
<apex:commandButton action="{!save}" value="Save" rerender="pblock"/>
<p />
</apex:pageBlock>
</apex:form>
</apex:page>
Controller
public class AddErrorCtrl {
Account account;
public PageReference save() {
if(account.NumberOfEmployees == null)
{
account.NumberOfEmployees.addError('You must enter a value.');
return null;
}
if(account.NumberofLocations__c == null)
{
account.NumberofLocations__c.addError('You must enter a value.');
return null;
}
try{
update account;
}
catch(DmlException ex){
ApexPages.addMessages(ex);
}
return null;
}
public String getName() {
return 'MyController';
}
public Account getAccount() {
if(account == null)
account = [select id, name, numberofemployees, numberoflocations__c from Account
where id = :ApexPages.currentPage().getParameters().get('id')];
return account;
}
}
Updates: Validate fields using Jquery
<apex:page controller="AddErrorCtrl" tabStyle="Account">
<style>
.error{ border:1px solid red !important;}
.messageClass{color: red; display: inline; float: right;position: absolute;margin: 3px;font-size:11px}
</style>
<apex:messages />
<apex:form >
<apex:pageBlock title="Hello {!$User.FirstName}!" id="pblock">
This is your new page for the {!name} controller. <br/>
You are viewing the {!account.name} account.
<p>
Number of Locations: <apex:inputText value="{!account.NumberofLocations__c}" id="Location_validation"/>
</p>
<p>
Number of Employees: <apex:inputText value="{!account.NumberOfEmployees}" id="Employee_validation"/>
</p>
<p>
<input type="button" value=" Save " onclick="validateFields();" class="btn"/>
<apex:actionFunction action="{!save}" name="SaveAF" rerender="pblock" />
</p>
</apex:pageBlock>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.3/jquery.js" ></script>
<script>
var isValidate = true;
function validateFields()
{
var varFieldlist = ["Location_validation", "Employee_validation"];
validateRequiredFields(varFieldlist);
if(isValidate)
{
SaveAF();
}
}
function validateRequiredFields(arrayFields){
$.each( arrayFields, function( i, val ) {
if($('[id$='+val+']').val() =='')
{
if(!$('[id$='+val+']').hasClass('error'))
{
$('[id$='+val+']').addClass('error');
$('[id$='+val+']').after("<p class='messageClass' id="+'__'+val+"> Required </p>");
isValidate = false;
}
}
else if($('[id$='+val+']').hasClass('error'))
{
$('[id$='+val+']').removeClass('error');
$('[id$=__'+val+']' ).remove();
isValidate = true;
}
});
}
</script>
</apex:form>
</apex:page>
