Learning XPages Part 35 : Advanced Validation Using RegEX

The validation for the JobTitle field was
fairly simple, we made sure it existed and that it was between 10 and 50
characters in length but what if you want to do some more advanced validation
like confirm that a phone number only contains numbers and the + and –
characters or that an email address contails the @ and . symbols? To do
this we need to create a ‘Constraints’ validation using RegEx. Regular
Expression is a method to identify patterns of characters in a string and
can range from simple to very complex. I won’t get into the entire RegEx
format here but I will provide a few commonly used RegEx Expressions.

Lets open the content_Person custom
Control again and select the ‘OfficePhoneNumber’ edit control. If you are
using server side validation then don;t forget to add in a ‘Display Error’
control and link it to the OfficePhoneNumber edit control and then in the
edit control’s properties disable the client side validation ( assuming
you didn’t set the entire app to Server Side Validation in the application
properties )

A picture named M2

I have also made the edit box control
a ‘Required Field’ by turning on the option in the validation tab or it’s

A picture named M3

Now go down to the ‘All Properties’
for the edit box control and look at the Data – Validators section. If
you click on it you’ll get a  small + and – button appearing in the
row and clicking on the + button will bring up the list of different validators
that you can add to the edit box

A picture named M4

We are going to use the xp:validateConstraint
validator and selecting it added the validator to the properties with fields
for the error message and the regex expression

A picture named M5

The RegEx that I am using I got from
a great resource called the RegExLibrary, This resource has lots of great
RegEx validations that you can use for your applications.
( Used Above ), Email
, US Zip
and many many more, just
be aware that there are multiple versions of the different expressions
and some may or may not do exactly what you need so always make sure you
test them out.

Save your Xpage and test editing the
persons phone number. Try adding a letter into the phone number and when
you save the page you should see your server side validation kick in and
receive an error message back.

A picture named M6

Now that we know how to validate the
data being entered we do need to be able to save it. In the next part we’ll
create our save button and also hide it when the document is not in edit

Tagged with: , , , ,
Posted in None
%d bloggers like this: