Regular Expression – Prevent All CAPS words in .net

The below formula will only accept strings that do not contain four or more consecutive uppercase letters.

@”^((?![A-Z]{4,}).\s*)*$”

Strings containing TLAs (Three letter acronyms) like SQL and USA are okay and will be accepted.

This can be used as part of a RegularExpressionValidator that would have code similar to below:

<asp:RegularExpressionValidator ID=”vld_txtDescription” runat=”server” SetFocusOnError=”true” ValidationExpression=”^((?![A-Z]{4,}).\s*)*$” ControlToValidate=”txtDescription” ErrorMessage=”Cannot use ALL CAPS words”  />

Note:If it is possible to crack the above regex, I will sent £20 to whoever does it.  Let me know in the Comment box.

For your interest, the unit tests for the above expression are below.  I apologise for WordPress being difficult with formatting:

using System;
using NUnit.Framework;
using System.Text.RegularExpressions;

namespace AllCapsPrevention
{
    [TestFixture]
    public class TestAllCapsRegex
    {
        private const string RegexValidString = @"^((?![A-Z]{4,}).\s*)*$";

        private Regex _regex;

        [SetUp]
        private void SettingUp()
        {
            _regex = new Regex(RegexValidString);
        }

        [TearDown]
        private void TearingDown()
        {
            _regex = null;
        }

        [Test]
        public void NoMatch____UUUUU_UUUUU____()
        {
            const string input = " UUUUU UUUUU ";
            Assert.IsFalse(_regex.IsMatch(input));
        }

        [Test]
        public void NoMatch_llllll_UUUU_llllllll()
        {
            string input = "llllll UUUU llllllll";
            Assert.IsFalse(_regex.IsMatch(input));
        }

        [Test]
        public void NoMatch_UUUUU_UUUUU()
        {
            const string input = "UUUUU UUUUU";
            Assert.IsFalse(_regex.IsMatch(input));
        }

        [Test]
        public void NoMatch_lllllllUUUUllllllllll()
        {
            const string input = "lllllllUUUUllllllllll";
            Assert.IsFalse(_regex.IsMatch(input));
        }

        [Test]
        public void NoMatch_llllllll_UUUUU_UUUUU_lllllllll()
        {
            const string input = "llllll UUUUU UUUUU lllllllll";
            Assert.IsFalse(_regex.IsMatch(input));
        }

        [Test]
        public void NoMatch_llllllllUUUUUlllllUUUUUlllllllll()
        {
            const string input = "llllllllUUUUUlllllUUUUUlllllllll";
            Assert.IsFalse(_regex.IsMatch(input));
        }

        [Test]
        public void NoMatch_llllllll_UUU_UUUU_lllllllll()
        {
            const string input = "llllllll UUU UUUU lllllllll";
            Assert.IsFalse(_regex.IsMatch(input));
        }

        [Test]
        public void Match_lllllll()
        {
            const string input = "llllll";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        public void Match_Ulllll_llllll()
        {
            const string input = "Ulllll_llllll.";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match_lllllll___________()
        {
            const string input = "llllll ";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match______lllllll_____()
        {
            const string input = " llllll ";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match______lllllll()
        {
            const string input = " llllll";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match______UUU_____()
        {
            const string input = " UUU ";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match____UUU__UUU_____()
        {
            const string input = " UUU UUU ";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match____UUU__UUU()
        {
            const string input = " UUU UUU";
            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match_Paragraph_No_Carriage_Returns()
        {
            string input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            input += "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match_Paragraph_Has_Carriage_Returns_Ends_With_Text()
        {
            string input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            input += Environment.NewLine + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match_Paragraph_Has_Carriage_Returns_Ends_With_Newline()
        {
            string input = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            input += Environment.NewLine + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            input += Environment.NewLine;

            Assert.IsTrue(_regex.IsMatch(input));
        }

        [Test]
        public void Match_Paragraph_Has_Carriage_Returns_Begins_With_NewLine()
        {
            string input = Environment.NewLine + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            input += Environment.NewLine + "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. ";

            input += Environment.NewLine;

            Assert.IsTrue(_regex.IsMatch(input));
        }
    }
}
Advertisements
This entry was posted in Technical and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s