Piotr Retecki
Piotr Retecki

Reputation: 1

Adding multiple conditions in regex

I am a beginner in using regex. I was trying to build a validation in SuccessFactors Onboarding 1.0. for a cell phone number field. People should be able to add up to 15 digits and the following characters: + ( ) and spaces. I want to disable people from entering letters (i.e. ext). If the format doesn't match, the system should throw an error. The error is configured within the page settings, and the regex is a condition, which when not matched causes the error popping up on the screen, hence the error message doesn't need to be a part of the expression.

I figured out the following code: ^[\d]{1,15}$|\|/[^a-z]+x/$, but that code only enables the validation of number of characters plus disables adding letters (when testing it seems to disable both, upper and lower cases). I don't know how to add a third condition which would allow adding the characters mentioned above. I tried with this code: [\d]{1,15}$|\[^a-z]+x\|\(?:+()\$, but this code doesn't allow for adding the additional characters plus disables the error message, user cannot continue processing to next page, but the error doesn't occurs on the screen, only the red asterisk next to the field.

Can I ask anyone to help me figuring out how to add the last piece of validation correctly, so that characters such as + () and up 15 digits are allowed, but everything else would pop up an error.

Thanks in advance!

Upvotes: 0

Views: 361

Answers (2)

Sarah Groß
Sarah Groß

Reputation: 10879

No worries, with the exception of a selected few, we're all beginners in using RegEx! šŸ˜…

On a general note, restricting what is considered to be a valid or invalid notation of a phone number might not be a good idea at all. Sometimes you'll see the extension number at the end being visually separated by a minus sign (as in "0123 45 678 - 9"). Some people might add the country code or dialling code in paranthesis, others won't.

Also consider numbers entered (or pasted from a source) with non-Western characters, as the chinese parenthesis ļ¼ˆ ļ¼‰ (they look similar, but are actually characters of their own with more whitespace around them!) - shouldn't those be considered valid as well?

If someone wants to enter an invalid number, they will anyway ("555-00000000" ...)!

But if you really want to restrict the string entered to numbers and the characters ()+- , this is the regular expression you'd need:

const rx = /^[0-9()+\- ]+$/;

// should be false:
console.log(rx.test("abc 123"));

// should be true:
console.log(rx.test("+49 123 456 789 - 0"));
console.log(rx.test("+49 (0) 123 456 789 - 0"));
console.log(rx.test("555-000 0000"));

// this will fail, because of the use of Chinese parenthesis,
// which is the only difference to the second number in the  previous block
console.log(rx.test("+49 ļ¼ˆ0ļ¼‰123 456 789 - 0"));

// of course, these would also be considered valid,
// unless you'd implement some more sophisticated checks
// - but is it really worth the trouble?
console.log(rx.test("((( 12345"));
console.log(rx.test("++++"));

Let me break the expression down for you:

/^[0-9()+\- ]+$/
 ^- beginning of the string
  ^- start of the character group
   ^- all the characters we want to allow
      (we have to escape the -, because otherwise
      it would denote an invalid character range)
            ^- end of the character group
             ^- + means: at least one, or more of those characters
              ^- end of the tring

A good place to build, check and verify your regular expressions, especially in the beginning, are regex testing tools like (in no specific order): regex101.com, regextester.com, or regexr.com

Upvotes: 1

Pentiux
Pentiux

Reputation: 215

pseudo code sort of

if (string.contains(Regex("[^\d\+\(\) ]")) throw Exception() else accept
if (string.length > 15 || string.isEmpty()) throw Exception() else accept

if you need more strict condition, make an exempla what is correct input, and what isn't correct

Upvotes: 1

Related Questions