I need to create a regex field validator on display name such that it should not have hyphen(-) unless if there is any date in the name.

Display Name Validation Error/ Allowed
This is a Display Name Allowed
This is a-Display Name Validation error. There is a "-" in between a and Display
01-01-2023 This is Display Name Allowed
01-01-2023 This is Display-Name Validation Error. There is a "-" in between Display and Name

This regex validates DATE: "^((19 | 20)[0-9][0-9])[- /.](0[1 - 9] | 1[012])[- /.](0[1 - 9] |[12][0 - 9] | 3[01])"

This regex validates "-"(HYPHEN): "[-]"

I don't know how to merge both and make my use cases work. Any experts here? Thanks in advance.

  • 1
    What value is there having a date in the name? A date field would be better for searching and reduces the likelihood you'll ever need to rename the item. Feb 1, 2023 at 13:58
  • You have regex that validates if it is a date. Why dont you add another check which just checks if it contains hyphen for e.g. if(string.Contains("-")){ if(string.matchesRegex){return true; } else {return false;}} return true; This is not code and simply written to explain the logic. You can improve it.
    – Ghan
    Feb 1, 2023 at 14:25
  • Can you explain your reasoning behind this approach? Feb 1, 2023 at 20:46
  • ChatGPT can help you with regex generation, however it is not clear why you would need validation in the first place, unless you're using the display name as a driver for real functionality such as URL generation. Feb 1, 2023 at 20:58
  • @MichaelWest: Thanks for responding. We have some articles where content authors need dates to be in their Display Name field. And as a best practice, we don't want hyphen in Names except dates as we are using Display Names for URL generation.. I am going to try out Marek's regex below.
    – Yeshwanth
    Feb 2, 2023 at 4:19

1 Answer 1


You can just join:

  • date + only not hyphens pattern:



  • only not hyphens pattern:


using or |

Final regex is:


Btw I changed the order of date and year in your pattern so it matches now 01-01-2023 instead of 2023-01-01 like in your regex.

  • Thank you. I will try it out.
    – Yeshwanth
    Feb 2, 2023 at 4:20
  • I can confirm the Regex is working Great !! Thanks again @Marek
    – Yeshwanth
    Feb 7, 2023 at 12:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.