<?xml version="1.0" encoding="utf-8"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><language>en</language><title>Blog posts by Matt Purdy</title> <link>https://world.optimizely.com/blogs/matt-purdy/</link><description></description><ttl>60</ttl><generator>Optimizely World</generator><item> <title>The versatility of using Regex in custom validation attributes.</title>            <link>https://world.optimizely.com/blogs/matt-purdy/dates/2021/2/the-versatility-of-using-regex-in-custom-validation-attributes/</link>            <description>&lt;p&gt;Hello there,&lt;/p&gt;
&lt;p&gt;To kick of my Episerver blogging activities I have decided to begin with the insight of a powerful combination that can be used to perform strict strongly typed server-side validation.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;This example showcases the power of Regex to perform required phone number field validation, which, in this instance was all numeric except for specific characters reserved for area codes.&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;After creating a validation Regex pattern object, you then use the Regex isMatch method to validate your field data, thus getting your result.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Validation Attribute&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;public class PhoneNumberValidationAttribute : ValidationAttribute
    {
        private readonly int _charLength;

        public PhoneNumberValidationAttribute(int charLength)
        {
            this._charLength = charLength;
        }

        public override bool IsValid(object value)
        {
            var inputtedVal = value as string;

            var validator = new Regex(@&quot;^[0123456789()+-]+$&quot;);

            var isValidInput = inputtedVal != null &amp;amp;&amp;amp; (validator.IsMatch(inputtedVal) &amp;amp;&amp;amp; inputtedVal.Length &amp;lt;= _charLength);

            return isValidInput;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var result = base.IsValid(value, validationContext);

            if (!string.IsNullOrWhiteSpace(result?.ErrorMessage))
                result.ErrorMessage = $&quot;Phone number must contain only numbers with the exception of the area code.&quot;;

            return result;
        }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Further improvements would be more complex pattern matching or enhanced error message overloading.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Model property decoration&lt;/strong&gt;&lt;/p&gt;
&lt;pre class=&quot;language-csharp&quot;&gt;&lt;code&gt;[PhoneNumberValidation(30)]
public virtual string TelephoneNumber { get; set; }&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Whilst writing this post I came to learn of certain alternative data type attributes available within .NET (System.ComponentModel.DataAnnotations) that would fit most use cases. However, this approach is a still a desirable one when chasing a certain level of autonomy.&lt;/p&gt;</description>            <guid>https://world.optimizely.com/blogs/matt-purdy/dates/2021/2/the-versatility-of-using-regex-in-custom-validation-attributes/</guid>            <pubDate>Thu, 11 Feb 2021 17:22:17 GMT</pubDate>           <category>Blog post</category></item></channel>
</rss>