+ Start a Discussion
Alex Wong 4Alex Wong 4 

Is there a way to check phone number format?

If I try the phone format in the field, it can provide a phone format.However, even I type in character, the system still accept it. If I use the number format, it can restrict users only to type number. However, the outlook of data will become a number format with this sign (","). How can I solve this problem? Thank you.
Best Answer chosen by Alex Wong 4
Onesh ReddyOnesh Reddy
 Hi Alex,
Yes, there is a way to check phone number format.

The below Validation Formula checks the phone number format as 999-999-9999.
1) Create a Validation rule for the PHONE Field.(Validates the Format of Data before saving)
2) Under Error Condition Formula add the syntax : NOT(REGEX(Phone, "[0-9]{3}-[0-9]{3}-[0-9]{4}"))
3) Give a Error message to display.
4) Save.

or 

The below Validation Formula that the Phone number is in (999) 999-9999 format.
This works by using the REGEX function to check that the number has ten digits in the (999) 999-9999 format.
Formula: NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
Error Message:US phone numbers should be in this format: (999) 999-9999.

Let me know if it helps you,

Best Regards,
Onesh.K
 

All Answers

Onesh ReddyOnesh Reddy
 Hi Alex,
Yes, there is a way to check phone number format.

The below Validation Formula checks the phone number format as 999-999-9999.
1) Create a Validation rule for the PHONE Field.(Validates the Format of Data before saving)
2) Under Error Condition Formula add the syntax : NOT(REGEX(Phone, "[0-9]{3}-[0-9]{3}-[0-9]{4}"))
3) Give a Error message to display.
4) Save.

or 

The below Validation Formula that the Phone number is in (999) 999-9999 format.
This works by using the REGEX function to check that the number has ten digits in the (999) 999-9999 format.
Formula: NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))
Error Message:US phone numbers should be in this format: (999) 999-9999.

Let me know if it helps you,

Best Regards,
Onesh.K
 
This was selected as the best answer
Alex Wong 4Alex Wong 4
@Onesh Reddy Sorry for forgetting to tell you that US phone format is not suitable for me. I want a 8-digit number checking. And I need to check them in the apex code level, but not the field setting level. 
if(artist.PhoneNumber__c != null){
                Pattern emailPattern = Pattern.compile('d\d?\d?\d?\d?\d?\d?\d?');
                Boolean isMatch = emailPattern.matcher(artist.Facebook__c).matches();
                if(!isMatch){
                    objAttachment.Body = null;
objAttachment2.Body = null;
objAttachment3.Body = null;
objAttachmentt.Body = null;
objAttachments.Body = null;
                    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR, 'Please provide a valid 8-digit phone number.'));
                    isValidUrl = false;
                }
            }

Is the above work?