You are viewing limited content. For full access, please sign in.

Question

Question

Help with a field constraint using regular expressions

asked on December 30, 2014

I need to create a template field with the following rule:

The field values can be any one of the following: 

 

XXXX (Being 1234) – All numeric

XXXX-XX (Being 1234-01) All numeric

XXXX-A (Being 1234-A) Four numeric and one alpha

 

Is this possible? I'm okay with the basic ones but this one is making me scratch my head. 

 

Thanks in advance. 

0 0

Answer

APPROVED ANSWER
replied on December 30, 2014 Show version history

Sure, it's possible. The thing you're looking for is called alternation "|" (think of it like an "OR"):

\d{4}|\d{4}-\d{2}|\d{4}-[A-Z]

This expression looks for any of the 3 cases you mentioned above (assuming you want capital letters A-Z only for that third case). This is just like taking 3 individual RegEx strings and sticking them together with OR.

Be sure to provide a clear constraint violation message to your users, because the generic one is totally uninformative.

2 0

Replies

replied on December 30, 2014

Thanks so much Scott, I totally forgot about the alternation. 

 

Much appreciated, happy holidays!

0 0
You are not allowed to follow up in this post.

Sign in to reply to this post.