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
SFDC@ErrorSFDC@Error 

Validation in Trigger

How can i validate mobile number should be 10 digit in trigger.I have created a trigger but its not working
trigger MobileNumber on Account (before update,before insert) {
 if( Trigger.isbefore && Trigger.isInsert)
    {
    for(Account l: Trigger.New){
        if(l.Mobile__c!=null && Mobile__c.lenght()=10){
alert ("Error");
        
    
      
        }
    }
}



}

 
Ajay Ghuge 6Ajay Ghuge 6
You can use Validation rule for the same : 
LEN(Mobile__c) < 10

For triggers correct the if statement 
 if(l.Mobile__c!=null && l.Mobile__c.length() !=10){
     l.adderror('Mobile number should be of length 10');
 }         

Kindly mark it as resolved.
Chetan KambleChetan Kamble
Use following code .....................phoneNumber should be string
        
        String numberWithoutSpclChars = phoneNumber.replaceAll('[^\\d]','');
        
        phoneNumber = numberWithoutSpclChars;
 
        if(phoneNumber.isNumeric())
        {
            if(phoneNumber.length() >= 10)
            {
                isValidNumber = true;
            }            
        }