Tech Tip: Enforce Property Formats Using Regular Expressions

Tech Tip: Enforce Property Formats Using Regular Expressions

There are times when you want to ensure that the values entered for a property follow a specific format. The most common example of this would be ensuring that phone numbers have the correct number of digits. Today we'll be covering how you can accomplish this in Aras Innovator without writing a single line of code!

Set Up

We'll start by signing in as an administrator and creating a new ItemType. Let's call it Client. Next, we'll add properties to keep tack of a few details about our Clients.

  1. Name
  2. Address
  3. Zip Code
  4. Phone Number

For simplicity, let's make all of these properties strings. We'll also allow Administrators to add new instances of this ItemType as well as see it on the TOC. Now select the new Client entry in the TOC and create a new Client. You should see a form like the one below.

This is certainly usable enough to pass on to our users, but without verifying our data, we're introducing the risk for human error which could result in data like below.

The Zip Code only has 4 digits and the Phone Number only has 8.

How can we ensure that our users must input Zip Codes that have 5 digits and Phone Numbers follow the format XXX-XXX-XXXX? While we could create a Server Event that calls a Method to check the formats of these properties, Aras Innovator supports a handy tool that removes the need to write any code for this use case.

Patterns and Regex

Let's go back to our Client ItemType and take a look at the Pattern column in the Properties grid. By writing a Regular Expression or Regex into this column, Innovator will ensure that all data for this property matches the Regular Expression. If you are new to Regular Expressions, there are a number of different helpful tutorials and guides you can find online including this one: https://www.regular-expressions.info/index.html .

Zip Code Regex

We want all of our zip codes to be exactly 5 characters. Each character should be a number between 0 and 9. With these rules, we can specify the Pattern of our Zip Code property to be the following Regular Expression: [0-9]{5}

Phone Number Regex

For simplicity in this example, we're going to make a few assumptions about the phone numbers we'll accept. All of our clients will have phone numbers from the United States of America. All phone numbers will include an area code and will follow the format XXX-XXX-XXXX. With these restrictions on our phone numbers, we can use the Regular Expression: \d{3}-\d{3}-\d{4} . The \d in this expression is shorthand for a digit between 0 and 9.

In Practice

Once these rules are in place, our users will no longer be able to input values that do not match the Regular Expression. Instead, they will be presented with a notification telling them the value is invalid and will prompt them to reenter the value.

Conclusion

Regular Expressions are very powerful and Aras Innovator makes it easy to leverage that power to ensure that the data in your environment is clean and consistent.

Subscribe to our blog and follow @ArasLabs on Twitter for more helpful content! You can also find our latest open-source projects and sample code on the Aras Labs GitHub page.

Also, if you enjoy these blog posts and want to see how other members of the Aras Community are using Innovator, consider registering for ACE, our annual community event!