+ Start a Discussion
GurunathGurunath 

Trigger for date validation

I have two fields one date_of _last_flight__c and date_of_last_sim__c in Aircraft_Contact__c object, these two fileds should be in date format like dd-mm-yyyy other wise i have to show the error like date should be in date format.........any one suggest me writing the validation trigger for this 

KodiKodi
Hi Gurunath,

Its difficult to write validation in trigger so can u use validation rule like this ('dd-mm-yyyy') try below code.

NOT(REGEX( date_of _last_flight__c ,'^(?:0[1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[-](?:0[1-9]|0[1-9]|1[0-2])[-](1[9][0-9][0-9]|2[0][0-9][0-9])$'))&&NOT(ISBLANK(DateOfBirth__c))
GurunathGurunath

thanks for your replay ...........however i cpoied what ur suggest like 

NOT(REGEX( date_of _last_flight__c ,'^(?:0[1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[-](?:0[1-9]|0[1-9]|1[0-2])[-](1[9][0-9][0-9]|2[0][0-9][0-9])$'))

 

but it is throwing the syntax error.......

@anilbathula@@anilbathula@
Hi Gurunath,

try this validation rule:-

NOT(REGEX(date_of _last_flight__c,"[0-9]{1,2}[-]{1}[0-9]{1,2}[-]{1}[0-9]{4}"))
GurunathGurunath

Hi,

 

 dont now whether i am right or wrong  and wrote a trigger like this

 

trigger Validation_Aircraft on Aircraft_Contact__c (before insert,before Update) {

public Integer th {get;set;}

public String X = '[0-9]{1,2}[-]{1}[0-9]{1,2}[-]{1}[0-9]{4}';


for(Aircraft_Contact__c AC: Trigger.new){

if(Ac.Total_Hours__c != Th && 'Ac.Date_of_Last_Flight__c' != X && 'AC.Date_of_Last_SIM__c '!= X){

Trigger.new[0].addError(' Hours In Numeric Format and Date should be in DD-MM-YYYY format ');
}
}
}

 

any one please suggest me wrong or right  here Date_of_Last_Flight__c,Date_of_Last_SIM__c are fields tpye is date.......

@anilbathula@@anilbathula@
Hi Gurunath,

You don't need a trigger for this validation rule will be enough.
Use the validation which i have given that's more than enough
GurunathGurunath

Thanx for your good suggestion......