Regular expression in Umbraco for number validation.

Posted by Vizioz Limited on Vizioz Umbraco Blog See other posts from Vizioz Umbraco Blog or by Vizioz Limited
Published on 2009-06-09T12:48:00.000-07:00 Indexed on 2010/12/06 17:00 UTC
Read the original article Hit count: 640

This evening I was looking for a way to validate an Umbraco node that could be either text or a numeric value, in my case a salary that could be either an hourly amount, an annual figure or a comment.

In the case where the node contained a value I wanted the XSLT to output a pound sign (£) and for any that contained text it would just output the text, as this could be something like "Contact Us" or "Negotiable"

I thought someone else might find this useful so here is the XSLT and the regular expression.

First if you are using Umbraco, don't forget to include the reference to the EXSLT Regular expression library at the top of your XSLT.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxml="urn:schemas-microsoft-com:xslt"
xmlns:umbraco.library="urn:umbraco.library"
xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions"
exclude-result-prefixes="msxml umbraco.library Exslt.ExsltRegularExpressions">

Then the code I used was:
<xsl:if test="Exslt.ExsltRegularExpressions:match($currentPage/data [@alias='Salary'],
'^[0-9]*\,?[0-9]*\.?[0-9]+$') != ''">
<xsl:text>£</xsl:text>
</xsl:if>

This regular expression allows any number of digits, an optional comma, more digits, an optional decimal point and finally more digits, so all the following are valid:

12,000
14.43
334,342.03

© Vizioz Umbraco Blog or respective owner

Related posts about Umbraco CMS

Related posts about umbraco scripts