0

I am trying to create a validation rule for US format number and using the following regex Validation

NOT(REGEX(Phone, "\\D*?(\\d\\D*?){10}"))

The above seems like a standard regex which will only allow saving for (000) 000-0000 format number.

However, if I enter characters in the following format (000) 000-0000yyy. It is still allowing save.

2
  • do you want your number only of 10 digits? Aug 31, 2018 at 8:28
  • I can add a length check to restrict it to 10 digits. What I want to understand is that why the regex is accepting the extra string characters, when Salesforce documentation is saying that the regex just allows number in US format which should actually be the format (000) 000-0000
    – Madhurima
    Aug 31, 2018 at 8:31

3 Answers 3

3

You can try the below one as this could make sure that the phone contains only numbers.

NOT( OR( ISBLANK(Phone), REGEX( Phone,"(\\D?[0-9]{3}\\D?)[\\s][0-9]{3}-[0-9]{4}")))
1

Phone validation is quite complex subject. You can check out the code and docs for libphonenumber to get some insight. I think using a libphonenumber port to parse/validate the number would give you a more precise result. I'm one of the authors of the library, so I can help you out in case you'll have any questions. Please note that regex will not handle all possible cases.

4
  • Please clearly indicate any affiliation or lack thereof you have with this product. Also, if all of your posts are for self promotion you may get flagged as a spammer even if each post individually conforms to the guidelines around self disclosure.
    – Adrian Larson
    Jul 30, 2020 at 17:02
  • @AdrianLarson, sorry - updated. Thank you for pointing out. The library is absolutely free, and was just done to help solve the common issue that people were having for a while now, with no other alternative library. We get nothing out of it.
    – ipetrovych
    Jul 30, 2020 at 17:13
  • I understand, and that's definitely part of why I haven't flagged anything. Free offerings deserve more leniency. As long as it's clear in your posts that you are affiliated, that's fine. Although your standing in the community will be higher if you contribute in other ways besides recommending your app. :)
    – Adrian Larson
    Jul 30, 2020 at 17:15
  • Noted! I'll try to correct this ;)
    – ipetrovych
    Jul 30, 2020 at 17:17
0

OR (AND ( NOT(ISBLANK(Phone)), OR (LEN ( Phone ) < 9, LEN( Phone ) > 15, NOT(REGEX(Phone, "[0-9 ]+"))) ) , AND ( NOT(ISBLANK(PersonMobilePhone)), OR (LEN ( PersonMobilePhone ) < 9, LEN( PersonMobilePhone ) > 15, NOT(REGEX(PersonMobilePhone , "[0-9 ]+"))) ) )

1
  • 2
    Why is this better than the accepted solution from 4 years ago? Where is this different? Nov 11, 2022 at 10:20

You must log in to answer this question.

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