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
Hariharan Sankaran 17Hariharan Sankaran 17 

validate name & dates

Hi All,

How to implement validation or trigger for name and date  field in salesforce

please share me idea or suggestion or sample code...

Thank you,
SIVASASNKARSIVASASNKAR
Hi Hariharan,

You can use below methods to validate the String: isAlpha(), isAlphaSpace(), isAlphanumeric(), isAlphanumericSpace()
For more information : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm
 
// String validation

String str = '1234';

System.debug('=>  '+str.isAlpha());
System.debug('=>  '+str.isAlphanumeric());
System.debug('=>  '+str.isAlphanumericSpace());


//Date validation
for (SObjectName obj: trigger.new) {
    String daveValue = '22012-13-12';
    Date dt = null;
    try{
      dt = Date.valueOf(daveValue);
    } Catch (Exceptoion eX){
      obj.addError(eX.getMessage());
    }
}

Thanks,
Sivasankar