regular expressions

  • Multiline TextBox Maximum Character Validation

    Maximum character validation on asp.net TextBox controls do not work, therefore a RegularExpressionValidator has to be used to make sure the maximum number of characters are not exceeded e.g. <asp:TextBox ID=”tbAbstract” runat=”server” Text='<%# Bind(“Abstract”) %>’ Rows=”5″ TextMode=”MultiLine” MaxLength=”4000″/><asp:RegularExpressionValidator ID=”valAbstract” runat=”server” EnableClientScript=”false” ControlToValidate=”tbAbstract” Text=”Abstract exceeds 4000 characters” Display=”Dynamic” SetFocusOnError=”true” ToolTip=”Abstract must not exceed 4000 characters” ValidationExpression=”^[sS]{0,4000}$”…

    Know More

  • Strip HTML Tags from a String using Regular Expressions 

    Whilst writing some code to search forum posts I had the reason to remove all HTML tags from the forum posts before I could do a valid phrase search. I created the following function: public static string StripDecodeHtml(string content){  content = HttpUtility.HtmlDecode(content);  string myTagPattern = @”]*>(?<text>.*?)“;  Regex myTagRegex = new Regex(myTagPattern, RegexOptions.Compiled          |RegexOptions.IgnoreCase|RegexOptions.Singleline);  // do until no more tags…

    Know More

  • Examples of Regular Expressions

    Trim string in JavaScript var trimre=’^s+|s+$’; // replace whitespacevar trimmedstring = string.replace(trimre,””); Check for a valid date format in JavaScript var re=’^(d{1,2})/(d{1,2})/(d{4})$’; // dd/mm/yyyyif (!datestring.match(re)){ alert(“Date” + datestring + ” not in valid dd/mm/yyyy format”);}else // valid date keyed in{ …valid date keyed} More to follow…

    Know More

  • Creating a Banned Words Filter

    I have a website with a user forum and content management system, where certain users (or anyone in the case of forums) can post to. I therefore had to create an easy way to filter and change certain naughty/controversial words from being viewed on my pages. I wanted the solution to be easy to maintain…

    Know More