How to limit a field "Phone Number" to only contain 10 digits?

  • 0
  • 1
  • Question
  • Updated 11 months ago
  • Answered
I have a field "Phone Number", I need the user to enter 10 digit phone number in the field. If the user enters digits less than or more than 10, it should display an error message. Is this possible using native functionality or needs some custom coding? I tried using Phone filed data type but it accepts any number of digits in the field. Any suggestions? 
Photo of Raj Helaiya

Raj Helaiya

  • 1,266 Points 1k badge 2x thumb

Posted 11 months ago

  • 0
  • 1
Photo of Evan Martinez

Evan Martinez, Community Manager

  • 8,814 Points 5k badge 2x thumb
Hi Raj,

In the past when I have users that have run into this issue, for native functionality, I have typically made use of a formula field and a form rule to accomplish this for data entry on a form. For that to work I set up a field (In this example lets call it Phone Number Check) as a formula checkbox field. For the Phone Number Check field I used a formula of:

If(Length([Phone Number])=10, True, False)

What this means is this checkbox field only checks when the phone number is specifically 10 digits long. Then you can set up a form rule which checks to see if that Phone Number Check field is checked and if it is not checked then you can set it to Abort the Save and give them a warning message that they must use a phone number that is a valid length of 10 digits.
Photo of Raj Helaiya

Raj Helaiya

  • 1,266 Points 1k badge 2x thumb
Hello Evan, thank you for the solution, I am not able to view the option of abort save though.
Photo of Evan Martinez

Evan Martinez, Community Manager

  • 8,814 Points 5k badge 2x thumb
Hi Raj,

In order to build a form rule you would want to open up the form to customize it and then select the Dynamic Form Rules tab. From there when creating a new rule you would want to set the condition to When the record is saved, and the Phone Number Check (or whatever field you want to check against) is not equal to checked, Then abort the save. I have included a screenshot below to show what an example form rule with this set up would look like.

Photo of Raj Helaiya

Raj Helaiya

  • 1,266 Points 1k badge 2x thumb
Thank you, the abort save activates when you go for "The Record" option so could not view it earlier. There is one more problem, when you use the above method, 2 messages are displayed: 1st: The <formula check field> is not equal to true(according to my condition) 
2nd : The custom message that I write. 

how can you display just the custom message? 
Photo of Alex Wilkinson

Alex Wilkinson

  • 1,016 Points 1k badge 2x thumb
Also, you may want to use a more elaborate check-box formula to handle entries containing non-digit characters and extraneous spaces. Something like the following, which handles up to five instances of a period, dash, parenthesis, or space entered as a separator between digits:

var text p=ToText(Trim([Phone Number]));
var text partOne=Part($p,1,".-)( ");
var text partTwo=Part($p,2,".-)( ");
var text partThree=Part($p,3,".-)( ");
var text partFour=Part($p,4,".-)( ");
var text partFive=Part($p,5,".-)( ");

Length($partOne & $partTwo & $partThree & $partFour & $partFive) = 10
Photo of Raj Helaiya

Raj Helaiya

  • 1,266 Points 1k badge 2x thumb
Can you please elaborate on the execution of the formula? As to, which formula field to use? I tried using the formula check box field and create the abort save option but it did not work. The formula field does not check when a 10 digit phone number is entered. 
Photo of Evan Martinez

Evan Martinez, Community Manager

  • 8,814 Points 5k badge 2x thumb
Hi Raj,

That formula Alex post does a more in depth look at the Length portion in order to do the Counting. It still needs the If statement that does the check for true and false. So something like

var text p=ToText(Trim([Phone Number]));
var text partOne=Part($p,1,".-)( ");
var text partTwo=Part($p,2,".-)( ");
var text partThree=Part($p,3,".-)( ");
var text partFour=Part($p,4,".-)( ");
var text partFive=Part($p,5,".-)( ");

If(Length($partOne & $partTwo & $partThree & $partFour & $partFive) = 10, True, False)

I believe should not just check the length of the number but check or uncheck that box for the form rule.
Photo of Raj Helaiya

Raj Helaiya

  • 1,266 Points 1k badge 2x thumb
This definitely worked. Thank you
Photo of Raj Helaiya

Raj Helaiya

  • 1,266 Points 1k badge 2x thumb
Thank you Alex, this helped.