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
Neeraj Sharma 103Neeraj Sharma 103 

How to validate Standard Phone Field of Account Object with Apex Class with validation is Only 10Digit is input by user

Hi 

I am new in Salesforce Begineer

My Question is:

How to validate Phone field of Account Object with Use of Apex Class and the validation is ONly 10 Digit is input by user no sepcial character no characters Only digits and digit will be 10 
and if user input digit according the condition then the record will be save and user not input 10digit then error will be show on the Phone field "Please Provide Valid Number"  on Custom Visual force Page
Please help me ASAP


Thanks 
Neeraj Sharma
 
Best Answer chosen by Neeraj Sharma 103
Khan AnasKhan Anas (Salesforce Developers) 
Hi Neeraj,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page id="pg" controller="PhoneValidationC" >
    <apex:form id="fm">
        
        <script >
        function show()
        {
            var phn=document.getElementById("{!$Component.pg.fm.pgb.pbs.one}");
            var pattern=/^\d{10}$/;
            if(!pattern.test(phn.value))
            {
                alert("Please Provide Valid Number");
                phn.focus();
                return false;
            }  
            else{
                alert("Apex Method");
                CallApexMethod();
                return true;
            }
        }
        </script>
        
        <apex:actionFunction name="CallApexMethod" action="{!insertIt}" reRender="fm"/>
        <apex:pageBlock id="pgb" title="Account Phone Validation">
            <apex:pageBlockSection columns="1" id="pbs">
                <apex:inputField value="{!acc.Name}" />
                <apex:inputField value="{!acc.Phone}" id="one" />               
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton value="Validate" onclick="show();" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class PhoneValidationC {
    
    public Account acc {get;set;}
    
    public PhoneValidationC(){
        acc = new Account();
    }
    
    public pageReference insertIt(){
        System.debug('Apex Method');
        INSERT acc;   
        return null;
    }    
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas

All Answers

v varaprasadv varaprasad
Hi Neeraj,

Try this validation rule : 
 
OR( 
LEN(Phone) != 10, 
NOT( 
OR( 
REGEX(Phone, "[0-9 ]+"), 
REGEX(Phone, "\\+[0-9 ]+")) 
) 
)

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.



Salesforce Freelance Consultant/Developer/Administrator/Trainer
@For Salesforce Project Support: varaprasad4sfdc@gmail.com

Salesforce latest interview questions and training videos :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


Thanks,
V Varaprasad
Email: varaprasad4sfdc@gmail.com

 
Khan AnasKhan Anas (Salesforce Developers) 
Hi Neeraj,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page id="pg" controller="PhoneValidationC" >
    <apex:form id="fm">
        
        <script >
        function show()
        {
            var phn=document.getElementById("{!$Component.pg.fm.pgb.pbs.one}");
            var pattern=/^\d{10}$/;
            if(!pattern.test(phn.value))
            {
                alert("Please Provide Valid Number");
                phn.focus();
                return false;
            }  
            else{
                alert("Apex Method");
                CallApexMethod();
                return true;
            }
        }
        </script>
        
        <apex:actionFunction name="CallApexMethod" action="{!insertIt}" reRender="fm"/>
        <apex:pageBlock id="pgb" title="Account Phone Validation">
            <apex:pageBlockSection columns="1" id="pbs">
                <apex:inputField value="{!acc.Name}" />
                <apex:inputField value="{!acc.Phone}" id="one" />               
            </apex:pageBlockSection>
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton value="Validate" onclick="show();" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class PhoneValidationC {
    
    public Account acc {get;set;}
    
    public PhoneValidationC(){
        acc = new Account();
    }
    
    public pageReference insertIt(){
        System.debug('Apex Method');
        INSERT acc;   
        return null;
    }    
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
This was selected as the best answer
Mohammed MohiddinMohammed Mohiddin
Hello Neeraj. 

I have made the code simple. Hope this helps.

This is your VisualForce Page
 
<apex:page  controller="EvaluatePhoneClass">
    <apex:form>
        
    <apex:pageblock>
        <apex:messages/>
        <apex:commandButton value="validate" action="{!validate}" />
        <br/><br/>
        Phone No:   <apex:inputText  value="{!phone}" />
        
        </apex:pageblock>
    </apex:form>
</apex:page>

This Will be your Controller 
 
public class EvaluatePhoneClass {

    public string phone {get;set;}
    public list<Account> acc{get;set;}
    public EvaluatePhoneClass()
    {
       acc = [select id, name,phone from Account limit 1];
    }
    public PageReference validate()
    {
         system.debug('phone.length()'+phone.length());
        if(phone.length()==10){
                    system.debug('if');
            
            acc[0].phone=phone;
            update  acc[0];
        }
        else{
            system.debug('else');
            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Enter Valid Phone Number');
            ApexPages.addMessage(msg);
            return null;
        }
        return null;
        
    }
}

Have A Nice Day
Deepali KulshresthaDeepali Kulshrestha
Hi Neeraj,

You can try this code
// here is your VF Page code

<apex:page controller="ValidatePhone">

    <apex:form>

    <apex:pageblock>

        <apex:messages/>

        <apex:commandButton value="validate" action="{!validate}" />

        <br/><br/>

        Phone No:   <apex:inputText value="{!phoneno }" />
     
        </apex:pageblock>

    </apex:form>

</apex:page>


// here is your Controller 

public class ValidatePhone{

 

    public string phoneno {get;set;}

    public list<Account> acc{get;set;}

    public ValidatePhone()

    {

       acc = [select id, name,phone from Account limit 1];

    }

    public PageReference validate()

    {

        if(phoneno .length()==10){         

            acc[0].phone=phoneno ;

            update  acc[0];

        } else{

            ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.ERROR, 'Enter Valid Phone Number');

            ApexPages.addMessage(msg);

            return null;

        }

        return null;
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha