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

Question

Question

Regular Expression for a field constraint with a variable character limit

asked on February 11, 2015

I have a customer that wants to have a constraint for a field but they want it to accept any of the following alterations: 

CAO-12/99

D-12/99

DR-12/99

 

I know you can use the alteration, but then it doesn't actually show the hyphen or the forward slash and the user must type it. Also, it lets them type as much as they want until they click OK and then they will get the constraint violation message. 

 

If I use: 

 

[A-Z][A-Z][A-Z]-\d\d/\d\d

 

This will work great but must accept 3 letters like CAO. I like that it shows the hyphen and the forward slash, and only lets them type 3 letters and 4 digits. 

 

Thoughts anyone? 

 

 

0 0

Replies

replied on February 11, 2015

You need to use some kind of "repetition" character in your regular expressions. A few simple options would be the "*" which represents 0 or more matches, the "+" which represents 1 or more matches, and "{n,m}" which matches anything repeated at least n but no more than m times. Here's how each of those would look:

 

[A-Z]*-\d\d/\d\d

[A-Z]+-\d\d/\d\d

[A-Z]{n,m}-\d\d/\d\d

 

Unfortunately, the more flexible your Regex is, the harder it is to tell when the constraint is violated, so these may still allow you to type as much as you want before giving a message.

0 0
replied on February 12, 2015

Hey Shaun,

 

If you use the expression:

[a-zA-Z]+-\d\d/\d\d

this will allow: any lower or upper case character, specifically one or more, then a hyphen, then 2 digits, slash, 2 digits.

To only allow upper case characters:

[A-Z]+-\d\d/\d\d

Either of these is suitable for your three examples provided. Feel free to reply if that doesnt quite match what you were looking for.

0 0
replied on February 12, 2015

Hello Shaun,

I want to clarify something about your question. Looking at your original post, I noticed this:

I know you can use the alteration, but then it doesn't actually show the hyphen or the forward slash and the user must type it

It sounds like you are already familiar with the options available to validate against variable input, such as [A-Z]{1,3}-\d\d/\d\d , and are asking if it possible to achieve the same variable input validation, but still show the hyphen and slashes in place while the user is typing.

The reason why including a wildcard or alternation no longer displays that information is that there is no longer a logical place to put the hyphen. Does it go after the first character? The third?

If you wish to always display the hyphen, would it be possible to use a padding character to make all the codes the same length?

If it's really important to have both the variable length and to not force users to type the hyphen and slash, an alternative would make the - and the / optional, then regularly run a workflow to fix up values that do not have the - and the /. For example, [A-Z]{1,3}-?\d\d/?\d\d makes the hyphen and the slash optional.

I'm not a regular workflow user, so I'm not going to suggest a workflow myself, but I would caution that if you go this route, you are careful with your starting rule to avoid bogging down workflow.

0 0
replied on February 12, 2015

The workflow might look something like this if your starting rule is a conditional whenever an entry with that field is created. 

It "detects" the presence of the special characters using regular expressions, then updates the field if they're not empty - using regular expressions to separate the letters and numbers, adding a hyphen between them and using formatting (##/##).

Note that this WF assumes the input is properly formatted besides the hyphen and slash.

Format.png
Format.png (16.58 KB)
1 0
replied on February 20, 2015

Sorry I'm just noticing these replies, I guess the subscription didn't work for notifications of replies on here :)

Thanks for all of your suggestions and details, I will give these a try and see what our client would like to stick with. Thanks again, much appreciated.  

0 0
replied on February 20, 2015

If you are not getting notified of replies to threads you've subscribed to, please check your Email Preferences and make sure that the option

Notify me when there are new replies to posts I'm subscribed to

is on and the notification interval is configured as desired.

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

Sign in to reply to this post.