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
Ganesh KalepuGanesh Kalepu 

Hi , guys! ..

can anybody say how to restrict users to enter only '10' digit Mobile number in <apex:inputtext> field in vf page & to display error message if they enter an incorrect mobile number???
Best Answer chosen by Ganesh Kalepu
Heather ThompsonHeather Thompson
Option 1
If you are saving to a record and are okay with having the message show up when the user attempts to save, you can use a validation rule (this would apply to the field in standard UI as well)

Option 2

In your apex class:
public void validate(){
     if (phoneNumber.trim().remove('-').remove('(').remove(')').length() != 10){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter a 10 digit phone number));
     }
}
In your Visualforce Page:
<apex:pageMessages id="errors">

<apex:inputText value="{!phoneNumber">
    <apex:actionSupport event="onChange" action="{!validate}" rerender="errors"/>

</apex:inputText>

Option3
Use Javascript

All Answers

Heather ThompsonHeather Thompson
Option 1
If you are saving to a record and are okay with having the message show up when the user attempts to save, you can use a validation rule (this would apply to the field in standard UI as well)

Option 2

In your apex class:
public void validate(){
     if (phoneNumber.trim().remove('-').remove('(').remove(')').length() != 10){
           ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter a 10 digit phone number));
     }
}
In your Visualforce Page:
<apex:pageMessages id="errors">

<apex:inputText value="{!phoneNumber">
    <apex:actionSupport event="onChange" action="{!validate}" rerender="errors"/>

</apex:inputText>

Option3
Use Javascript
This was selected as the best answer
Sharad SoniSharad Soni
Hi Ganesh,

Restrict the user by entering this code.

   
        <apex:pageBlock id="thePageBlock"> 
            <apex:inputText maxlength="10" label="Mobile" />
            <apex:commandButton value="Press" />
Sharad SoniSharad Soni
To show the error message  bind the inputtext tag to some controller variable and check for undesired character in the variable and 
show the apex message.

Thanks.
Regards
Sharad.