How can I use XSLT 2.0 to validate an email-address? Is there a predefined method or do I have to build a regex-expression?
Thanks
How can I use XSLT 2.0 to validate an email-address? Is there a predefined method or do I have to build a regex-expression?
Thanks
You can do this in XSLT 2.0 but you will have to use a regular expression (although writing a regular expression that can actually match all possible email addresses is another matter).
In the case of using a regular expression, you would use the matches function to check if a string matches the regular expression for an email address (where $email is a variable containing the string to validate. Alternatively you could use element name containing the email address).
<xsl:if test="matches(upper-case($email),'^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$')">
Valid
</xsl:if>
Check out http://www.xml.com/pub/a/2003/06/04/tr.html for information on regular expressions.
Note that I got the regular expression from this example from http://www.regular-expressions.info/email.html. In this case, it assumed it was upper-case, hence the use of the function in my example.
Yes, you can use XPath 2.0's match() function in XSLT 2.0 to test a candidate email address against a regular expression.
However, whether it is a good idea to use regular expressions to validate an email address is another question. See the cited SO question and answers for candidate regular expressions as well as warnings about the limitations of using regular expressions to validate emails addresses in general.