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
mac adminmac admin 

How to validate the phoen number in custom vfpage

Hi all,
Canyone help me how to validate the phone number in custom vfpage bfeore redirecting to next page. I'm tried with the standard validation but it fires after submitting the record.

Thanks in advance.


Hi Mac,

Please use Jquery or javascript validation method, this will validate the phone field on VF page itself. 


SKype: gaurav62990

mac adminmac admin
Hi Gaurav,
Can you explain how to use java script in hte vfpage.
Hi Mac,

Please check this blog for understanding basic concept of Jquery validation in custom pages.

Hope this helps !!!

vikas rathi91vikas rathi91
I am work on it And validate the Us mobile number in my VF page use my sample code(Java script) 

vf page code

<apex:inputText value="{!SearchNumber}" id="txt1" styleClass="input-element" onchange="validatenumber()"/>
<script type="text/javascript"> function validatenumber(){ var inputtxt = document.getElementById('p:ff:txt1'); var phoneno = /^(1\s|1|)?((\(\d{3}\))|\d{3})(\-|\s)?(\d{3})(\-|\s)?(\d{4})$/; var phoneno1 = /^(1\s|1|)?((\(\d{3}\))|\d{3})(\.|\s)?(\d{3})(\.|\s)?(\d{4})$/; if(inputtxt.value.match(phoneno) || inputtxt.value.match(phoneno1)){ return true; } else { alert('Enter Valid number'); inputtxt.value = ''; return false; } } </script>
mac adminmac admin
Hi Vikas,
Thanks for the replay. I want to show the error message than showing an alert. Is it possible to show the error message than a alert ,,,?
vikas rathi91vikas rathi91
Hello Mac admin

  I am not sure that show the error message first then alert. I thing it is possible through Java script or jquery . 
I thing you try java script to show the msg then alert

Vikas Rathi
Perfect HelpPerfect Help
I am not using the validation using visual-force page yet. But i read from the method from a book and i hope you can do a regax validation in javascript to validate the number. I have used this regex. For more visit essay writing service reviews (
vishnu Rvishnu R

<apex:page id="pg" controller="opportunityvalidateclass" >
    <apex:form id="fm">
        <script >
        function show()
            var phn=document.getElementById("{!$}");
            var pattern=/^\d{10}$/;
                alert("Mobile should be 10 digits");
                return false;
        <apex:pageBlock id="pgb" title="Opportunity validation">
            <apex:pageBlockSection columns="1" id="pbs">
                <apex:inputText label="Opportunity Name"  value="{!Name}" />
                <apex:inputText label="Opportunity phone"  value="{!phone}" id="one" />
                <apex:inputText label="Opportunity Pincode"  value="{!pincode}"  />                
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton value="validate" onclick="show()" />
                <apex:commandButton value="save"  />


public class opportunityvalidateclass {
    public string Name{set;get;}
    public string phone{set;get;}
    public string pincode{set;get;}
    public string email{set;get;}
    public void insertit(){
        Opportunity o=new Opportunity();
        insert o;

hope this helps you..

Vishnu R