254 shaares
4 results
tagged
validation
I'm trying to write a regular expression to validate US phone number of format (123)123-1234 or 123-123-1234 as true.
The easiest way to match both this (000)000-0000 is this
^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$
and this 000-000-0000 is this
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
and to use alternation ((...|...)): specify them as two mostly-separate options:
^(\([0-9]{3}\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$
By the way, when Americans put the area code in parentheses, we actually put a space after that; for example, I'd write (123) 123-1234, not (123)123-1234. So you might want to write:
^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$
(Though it's probably best to explicitly demonstrate the format that you expect phone numbers to be in.)
The easiest way to match both this (000)000-0000 is this
^\([0-9]{3}\)[0-9]{3}-[0-9]{4}$
and this 000-000-0000 is this
^[0-9]{3}-[0-9]{3}-[0-9]{4}$
and to use alternation ((...|...)): specify them as two mostly-separate options:
^(\([0-9]{3}\)|[0-9]{3}-)[0-9]{3}-[0-9]{4}$
By the way, when Americans put the area code in parentheses, we actually put a space after that; for example, I'd write (123) 123-1234, not (123)123-1234. So you might want to write:
^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$
(Though it's probably best to explicitly demonstrate the format that you expect phone numbers to be in.)
Formula for format 999-999-9999:
=AND(
LEN([num])=12,
IF(ISERROR(FIND("-", [num],4)),
FALSE,
(FIND("-", [num], 4) = 4)
),
IF(ISERROR(FIND("-", [num],8)),
FALSE,
(FIND("-", [num], 8) = 8)
),
IF(ISERROR(1*CONCATENATE(MID([num], 1, 3), MID([num], 5, 3), MID([num], 9, 4))),
FALSE,
AND(
1*CONCATENATE(MID([num], 1, 3), MID([num], 5, 3), MID([num], 9, 4)) > 1000000000,
1*MID([num], 1, 3) <> 911,
1*MID([num], 5, 3) <> 911
)
)
)
=AND(
LEN([num])=12,
IF(ISERROR(FIND("-", [num],4)),
FALSE,
(FIND("-", [num], 4) = 4)
),
IF(ISERROR(FIND("-", [num],8)),
FALSE,
(FIND("-", [num], 8) = 8)
),
IF(ISERROR(1*CONCATENATE(MID([num], 1, 3), MID([num], 5, 3), MID([num], 9, 4))),
FALSE,
AND(
1*CONCATENATE(MID([num], 1, 3), MID([num], 5, 3), MID([num], 9, 4)) > 1000000000,
1*MID([num], 1, 3) <> 911,
1*MID([num], 5, 3) <> 911
)
)
)
Formula for field named [Mobile]:
=AND(IF(ISERROR(FIND("(", [Mobile],1)), FALSE, (FIND("(", [Mobile]) = 1)), IF(ISERROR(FIND(")", [Mobile],5)), FALSE, (FIND(")", [Mobile], 5) = 5)), IF(ISERROR(FIND(" ", [Mobile],6)), FALSE, (FIND(" ", [Mobile], 6) = 6)), IF(ISERROR(FIND("-", [Mobile],10)), FALSE, (FIND("-", [Mobile], 10) = 10)),IF(ISERROR(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4))), FALSE, AND(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4)) > 1000000000, 1*MID([Mobile], 2, 3) <> 911, 1*MID([Mobile], 7, 3) <> 911)))
Also see, https://thechriskent.com/2012/08/15/validate-phone-number-columns-in-sharepoint/
=AND(IF(ISERROR(FIND("(", [Mobile],1)), FALSE, (FIND("(", [Mobile]) = 1)), IF(ISERROR(FIND(")", [Mobile],5)), FALSE, (FIND(")", [Mobile], 5) = 5)), IF(ISERROR(FIND(" ", [Mobile],6)), FALSE, (FIND(" ", [Mobile], 6) = 6)), IF(ISERROR(FIND("-", [Mobile],10)), FALSE, (FIND("-", [Mobile], 10) = 10)),IF(ISERROR(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4))), FALSE, AND(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4)) > 1000000000, 1*MID([Mobile], 2, 3) <> 911, 1*MID([Mobile], 7, 3) <> 911)))
Also see, https://thechriskent.com/2012/08/15/validate-phone-number-columns-in-sharepoint/
Formula for field named [Mobile]:
=AND(IF(ISERROR(FIND("(", [Mobile],1)), FALSE, (FIND("(", [Mobile]) = 1)), IF(ISERROR(FIND(")", [Mobile],5)), FALSE, (FIND(")", [Mobile], 5) = 5)), IF(ISERROR(FIND(" ", [Mobile],6)), FALSE, (FIND(" ", [Mobile], 6) = 6)), IF(ISERROR(FIND("-", [Mobile],10)), FALSE, (FIND("-", [Mobile], 10) = 10)),IF(ISERROR(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4))), FALSE, AND(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4)) > 1000000000, 1*MID([Mobile], 2, 3) <> 911, 1*MID([Mobile], 7, 3) <> 911)))
Also see, https://thechriskent.com/2012/08/15/validate-phone-number-columns-in-sharepoint/
=AND(IF(ISERROR(FIND("(", [Mobile],1)), FALSE, (FIND("(", [Mobile]) = 1)), IF(ISERROR(FIND(")", [Mobile],5)), FALSE, (FIND(")", [Mobile], 5) = 5)), IF(ISERROR(FIND(" ", [Mobile],6)), FALSE, (FIND(" ", [Mobile], 6) = 6)), IF(ISERROR(FIND("-", [Mobile],10)), FALSE, (FIND("-", [Mobile], 10) = 10)),IF(ISERROR(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4))), FALSE, AND(1*CONCATENATE(MID([Mobile], 2, 3), MID([Mobile], 7, 3), MID([Mobile], 11, 4)) > 1000000000, 1*MID([Mobile], 2, 3) <> 911, 1*MID([Mobile], 7, 3) <> 911)))
Also see, https://thechriskent.com/2012/08/15/validate-phone-number-columns-in-sharepoint/