Blog

How to Validate Data Object Properties With Regular Expressions

by on September 1, 2019

When saving or updating a data object in a persistent storage, it is often necessary to make sure a property follows a particular data format. For instance, properties like email address, a US phone number, a bank account number, a URL and many others may be validated with regex (a regular expression). Backendless makes it very easy to do that.

For any property, or any column to be precise, you can specify a regex that will be used to validate incoming values. The values are validated when an object is saved or updated. If the validation fails, so does the API operation. Consider the following example:

The table below has the SSN property which will store US social security numbers. To validate the values stored in the table, we will use a regular expression.
Notice the validator in the Custom regex field:
^(\d{3}-?\d{2}-?\d{4}|XXX-XX-XXXX)$

The regular expression validates any US social security number in the XXX-XX-XXXX format. Once the column is saved, try entering a value for the column using Backendless Console or the API. Saving or updating any value which does not follow the format above would result in an error.

Leave a Reply