Regular expression in Umbraco for number validation.

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

 

 


Author

Chris Houston

Chris Houston

Chris is the founder of Vizioz and has been working in the field of website development for over 20 years. Chris has been working with Umbraco since 1998 and now spends his time running both Vizioz and QV Offices.


comments powered by Disqus