• WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG
    • WHO WE ARE
    • WHAT WE DO
      • Salesforce
        • Implementations
          • Sales Cloud
          • Service Cloud
          • CPQ
          • Field Service Lightning
          • Field Service for SMEs
        • Developments
          • Salesforce Customization
          • Custom Application Development
          • AppExchange Product Development
        • Migrations
          • Classic to Lightning Migration
          • Other Systems to Salesforce Migration
        • Integrations
      • Data Science
        • BI Solutions
        • AI/ML solutions
        • Agentic AI
    • HOW WE DO
      • Delivery Model
      • Our Works
    • REACH US
      • Contact Us
      • Careers
    • BLOG
  • [email protected]
  • (+91) 44-49521562
Merfantz - Salesforce Solutions for SMEs
Merfantz - Salesforce Solutions for SMEs
  • WHO WE ARE
  • WHAT WE DO
    • Salesforce
      • Implementations
        • Sales Cloud
        • Service Cloud
        • CPQ
        • Field Service Lightning
        • Field Service for SMEs
      • Developments
        • Salesforce Customization
        • Custom Application Development
        • AppExchange Product Development
      • Migrations
        • Classic to Lightning Migration
        • Other Systems to Salesforce Migration
      • Integrations
    • Data Science
      • BI Solutions
      • AI/ML solutions
      • Agentic AI
  • HOW WE DO
    • Delivery Model
    • Our Works
  • REACH US
    • Contact Us
    • Careers
  • BLOG

Validation Rules on the VF page for Phone and Name in Salesforce

  • July 27, 2017
  • Merfantz Editor
  • Salesforce Developments Tutorial
  • 0

We want to validate the phone field that it should have 10 digits only and the last name should have text only so how to use the extension.

I hope below code should help you…..

Visualforce Page:

<apex:page standardController="Contact"  sidebar="false" docType="html-5.0">
  <apex:form >
    <apex:pageBlock title="Edit Contact">
      <apex:pageBlockSection columns="1">
         <apex:inputField value="{!Contact.FirstName}"/>
         <apex:inputField id="lastname" value="{!Contact.LastName}" onchange="allLetter();" />
         <div id="err2" style="margin-left: 450px;margin-top: -28px;" ></div>
         <script type="text/javascript">
            function allLetter()  
             {                
                var letters = /^[A-Za-z]+$/;  
                var inputtxt=document.getElementById("j_id0:j_id1:j_id2:j_id3:lastname").value;
                if(!inputtxt.match(letters))  
                {  
                  document.getElementById("err2").innerHTML="Last Name must be text only";
                }else{
                  document.getElementById("err2").innerHTML="Valid Name";
                }                
              }  
         </script>
         <apex:inputField value="{!Contact.Email}"/>
         <apex:inputField value="{!Contact.Birthdate}"/>
         <apex:inputField id="phone" value="{!Contact.Phone}" onchange="checkPhone();" />
         <div id="err1"></div>
          <script type="text/javascript">
              function checkPhone(){                 
                 var phone=document.getElementById("j_id0:j_id1:j_id2:j_id3:phone").value;
                 var letters = /^[A-Za-z]+$/;
                 if(phone.length!=10 || phone.match(letters))
                    document.getElementById("err1").innerHTML="Phone must be 10 digits long. OR Can not contain Letters.";
                 else
                    document.getElementById("err1").innerHTML="Valid Phone Number";
              }
          </script>        
      </apex:pageBlockSection>      
    <apex:pageBlockButtons >
         <apex:commandButton action="{!save}" value="Save"/>
    </apex:pageBlockButtons>   
    </apex:pageBlock>  
   </apex:form>   
</apex:page>

Result:-

We are the ISV Partners and Please reach us for custom development => www.merfantz.com

---------------------- We Are Happy To Helping You -----------------

Author Bio

Merfantz Editor
+ Recent Posts
  • August 4, 2023
    How to Setup CRM Analytics for Financial Services Cloud
  • August 2, 2023
    How to Get Started with Salesforce Hyperforce - Merfantz
  • July 28, 2023
    Health Cloud Mastery: Salesforce Trailhead for Effective Healthcare CRM
  • July 26, 2023
    How to integrate Data cloud and Marketing cloud In Salesforce
Tags: Field Service ManagementFieldax
  • Previous How can I Display Superscript on Date Format using VisualforcePage
  • Next Create Tables Display on Detail Page by Using VisualForce

Leave a Reply

You must be logged in to post a comment.

Merfantz Technologies is a leading Salesforce consulting firm dedicated to helping small and medium enterprises transform their operations and achieve their goals through the use of the Salesforce platform. Contact us today to learn more about our services and how we can help your business thrive.

Discover More

Terms and Conditions
Privacy Policy
Cancellation & Refund Policy

Contact Info

  • No 96, 2nd Floor, Greeta Tech Park, VSI Industrial Estate, Perungudi, Chennai 600 096, Tamil Nadu, INDIA
  • (+91) 44-49521562
  • [email protected]
  • 9:30 IST - 18:30 IST

Latest Posts

Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder
Unlocking the Power of Dynamic Forms in Salesforce Lightning App Builder May 29, 2025
Einstein Activity Capture Boosting Productivity & Relationship Intelligence
Einstein Activity Capture: Boosting Productivity & Relationship Intelligence May 22, 2025
Let Einstein Help You Build - Salesforce Flow
Let Einstein Help You Build – Salesforce Flow May 15, 2025

Copyright @2023 Merfantz Technologies, All rights reserved