0

I have very little experience in apex classes/visualforce pages, so hopefully I can get some help updating an existing code in our org.

Current code:

<apex:pageBlockSectionItem >
     Home Phone: <apex:inputtext value="{!PGPhone}"/>
</apex:pageBlockSectionItem>

PGPhone is String data type in the controller so when I tried the input type="number", it errored out incompatible data type.

8
  • You seem to be missing your code, can you edit your post to update it with what you have now?
    – Jayant Das
    May 9, 2018 at 15:52
  • The body of my post has the code on it, but it seems anything with <> tags were redacted
    – Hazel
    May 9, 2018 at 16:03
  • Use code formatter to be able to post it.
    – Jayant Das
    May 9, 2018 at 16:04
  • @Hazel, you just needed a blank line before your code block. I fixed it.
    – David Reed
    May 9, 2018 at 16:04
  • So are you looking this to be saved as standard format on a phone field in backend? Or you are looking this to be displayed on your UI as standard phone format?
    – Jayant Das
    May 9, 2018 at 16:08

1 Answer 1

0

When possible, try using apex:inputField instead:

<apex:inputField value="{!contactRecord.Phone}" />

This should automatically invoke the default phone-formatting behavior of the UI.

Note that "number" does not mean "phone number", it means "a numeric value" (digits only).

Otherwise, you might write some logic on your field in JavaScript:

 <apex:inputText value="{!PGPhone}" onblur="formatPhone(this)" />

You can find many examples of formatting a phone number using JavaScript on the Internet; it depends on how particular you want to be, which may be anything from i18n formatting to simple US-only formats.

4
  • Hi @sfdfox, I did try that and it worked, but then we are using this one visualforce page to accept several contacts (Primary Guardian, Secondary Guardian, etc) and they all use the same Home Phone standard fields. So maybe this is the reason why the they were created variables in the controller for each phone number.
    – Hazel
    May 9, 2018 at 16:34
  • To do that, you can create several Contact record variables, or even a list of Contact records you could iterate over. You might want to think more about your design. Leveraging apex:inputField to gain default behavior is preferable.
    – sfdcfox
    May 9, 2018 at 16:35
  • Can you give an example how to do that? Each phone already have variables (ex. PGPhone, SGPhone, etc). They were declared Public String in the controller and inserted as PG_ct.HomePhone = PGPhone. I tried the apex:inputField value="{!PGPhone}" but an error says inputField value cannot bind into non SObjects.
    – Hazel
    May 9, 2018 at 16:44
  • @Hazel Correct, you'd need to declare contact variables, like public Contact pg { get; set; }, which you'd reference in your Visualforce as apex:inputField value={!pg.phone}"
    – sfdcfox
    May 9, 2018 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .