Regular Expressions in Rules

1 minute read time.
I was just answering an email about how a consultant could create a business rule that ensures data entered by the user matches a particular pattern. The specific example was needed to confirm that the code entered into a text field was only numeric and didn't include alphabetic characters.

I had started to write that the consultant should consider using a regular expression to check the pattern of the string entered. I was convinced that I had discussed this in an earlier post and am surprised that I haven't.

I like regular expressions as they are efficient ways of coding checks on strings.

Quite a good introductory article about Regular Expressions in general can be found here:

http://en.wikipedia.org/wiki/Regular_expression

a more detailed discussion of regular expressions in Javascript can be found here:

http://msdn2.microsoft.com/en-us/library/1400241x%28vs.80%29.aspx

But anyway the simple example I sent my enquirer was this


On Change


This function is placed in the Screen custom content and called from onchange script of field.

checkCustomerref(this.value);

function checkCustomerref (x)
{
re = /\d{8}/;
r = x.match(re)
if (!r)
{
window.alert('BM Customer Ref must be 8 Numbers');
}
}


On Validate rule


re = /\d{8}/;
r = Values('comp_customerref').match(re)
if (!r)
{
Valid = false;
ErrorStr = 'BM Customer Ref must be 8 Numbers';
}