+ Start a Discussion
naveen kunurunaveen kunuru 

custome validation

custom validation to check field is not empty while saving, if field is empty display error message
here is my code  is this code is ok?please let me known any allternation code from u guys
<apex:page controller="task43" sidebar="true" showHeader="true">
<apex:form >
<apex:pageBlock title="homepage" id="block1">
<apex:pageBlockSection >
<apex:pagemessages >
</apex:pagemessages>
<apex:inputText value="{!f1}"/>
<apex:inputText value="{!f2}"/>
<apex:commandButton value="login" action="{!log}" reRender="block1"/>

</apex:pageBlockSection>




</apex:pageBlock>



</apex:form>
</apex:page>



public with sharing class task43 {

    public PageReference log() {
    if(f1==''||f1==null&&f2==''||f2==null){
    apexpages.addmessage(new apexpages.message(apexpages.severity.warning,'please enter'));
    }
    
        return null;
    
    }


    public String f2 { get; set; }

    public String f1 { get; set; }
}
Best Answer chosen by naveen kunuru
Dushyant SonwarDushyant Sonwar
Hi naveen,
Try this condition
if(f1.trim().length()==0 || f2.trim().length()==0)
It will first remove the blank spaces and then check the length of string..

All Answers

Pablo VenturinoPablo Venturino
Couple points with your code:
  1. Your condition is a little complex, you may want to use parenthesys to make sure it executes correctly.
  2. Consider using String.isBlank(f1) instead of manually checking all possible blank values. String.isBlank considers all cases.
  3. It seems your error condition will only fire if both fields are blank at the same time. Is that what you expect?
naveen kunurunaveen kunuru
hey hi,i thniks if i use OR instead of AND will it work if either one field is empty
Dushyant SonwarDushyant Sonwar
Hi naveen,
Try this condition
if(f1.trim().length()==0 || f2.trim().length()==0)
It will first remove the blank spaces and then check the length of string..
This was selected as the best answer
naveen kunurunaveen kunuru
hey thanks ,i got that but will u let me known, is this the only way to display error message 
apexpages.addmessage(new apexpages.message(apexpages.severity.warning,'please enter'));