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
abu saleh khan 8abu saleh khan 8 

I want to validate on a phonennumber field, if phone number starts with 7,8,9 then only it should get inserted in to my sobject.How to achive it using apex?

Vf 

<apex:page Controller="Task39PhoneValidation">
  <apex:sectionHeader title="Student" subtitle="Home"/>
  <apex:pagemessages />
  <apex:form >
    <apex:pageblock tabstyle="Student__c">
      <apex:pageblockbuttons >
        <apex:commandButton value="Save" action="{!submit}"/>
        <apex:commandButton value="Save&New" action="{!saveandnew}"/>
        <apex:commandButton value="Cancel" action="{!cmeback}"/>
      </apex:pageblockbuttons>
      <apex:pageBlockSection >
        
      <apex:pageBlockSection title="Student Details" >
        <apex:inputField value="{!st.Name}"/>
        <apex:inputField value="{!st.Last_Name__c}"/>
        <apex:inputField value="{!st.Dob__c}"/>
      </apex:pageBlockSection>
      <apex:PageBlockSection title="Additional Details">
        <apex:inputField value="{!st.Tenure__c}"/>
        <apex:inputField value="{!st.Course__c}"/>
        <apex:inputField value="{!st.Doctor__c}"/>
      </apex:PageBlockSection>
      <apex:pageBlockSection title="Contact Details">
        <apex:inputField value="{!st.Address__c}"/>
        <apex:inputField value="{!st.Permanent_Address__c}"/>
        <apex:inputField value="{!st.Mobile__c}"/>
      </apex:pageBlockSection>
      </apex:pageBlockSection>
    </apex:pageblock>
  </apex:form>
</apex:page>




Controller:

public with sharing class Task39PhoneValidation {
    
    Public Integer phn;
    
    public Task39PhoneValidation(){
        st = new Student__c();
        
    }

    public PageReference cmeback() {
        return null;
    }


    public PageReference saveandnew() {
        return null;
    }


    public PageReference submit() {
        phn = st.mobile__c.length();
        if(phn == 10 ){
            insert st;
        }
        else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Your Phone number should have Only 10 digits not more than that or lesser then that .'));
        }
        
        return null;
    }


    public Student__c st { get; set; }
}
Best Answer chosen by abu saleh khan 8
v varaprasadv varaprasad
Hi Abu,

Please check below sample code:
string phn = '8234567891';
        if(phn.length() == 10 && (phn.startsWith('9') || phn.startsWith('8') || phn.startsWith('7'))){
            system.debug('Phone nember stated with 7 or 8 or 9');
			//insert st;
        }
        else{
		 system.debug('Phone nember not stated with 7 or 8 or 9');
           // ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Your Phone number should have Only 10 digits not more //than that or lesser then that .'));
        }



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

Thanks
Varaprasad
@For Support: varaprasad4sfdc@gmail.com