You need to sign in to do that
Don't have an account?
abu 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; }
}
<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; }
}
Please check below sample code:
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