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.

Regards,
mac.
GauravGargGauravGarg

Hi Mac,

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

Thanks,

Gaurav
SKype: gaurav62990

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

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

https://www.sitepoint.com/basic-jquery-form-validation-tutorial/

Hope this helps !!!

Thanks,
Gaurav
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

Thanks
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 (http://essaywriting-servicereviews.com/)
vishnu Rvishnu R
hi,

VF PAGE:
<apex:page id="pg" controller="opportunityvalidateclass" >
    <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("Mobile should be 10 digits");
                phn.focus();
                return false;
            }  
        }
        </script>
        <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:pageBlockSection>
            <apex:pageBlockButtons location="Bottom">
                <apex:commandButton value="validate" onclick="show()" />
                <apex:commandButton value="save"  />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

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();
        o.Name=Name;
        o.shobithapp__TrackingNumber__c=phone;
        insert o;
        
    }    
}

hope this helps you..

Thanks
Vishnu R