Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 238429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:30:17+00:00 2026-05-11T20:30:17+00:00

Does anyone have a good way of finding if a string contains malformed XHTML

  • 0

Does anyone have a good way of finding if a string contains malformed XHTML using Javascript?

Since my page allows ‘user’ generated XHTML returns (the users can be trusted) and injects it into the DOM, I want a way to check if there are unclosed or overly closed tags, and encode them as < and > so that it will simply display the errors as text. This way all valid xhtml will still be displayed, and the invalid will simply become text nodes, allowing the script to at least continue with the errors.


Here’s a similar method I made, which is rather crude. It has a different purpose (simply stripping all valid xhtml tags and leaving the rest. It works by recursively selecting the inner most tags and stripping them out.

stripHTML: function(html) {
   var validXHTML = /<(\S+).*>(.*?)<\/\1>/i;
   var validSelfClose = /<(input|img|br|hr)[^>]*\/>/gi;

   html = html.replace(validSelfClose, '');

   if(validXHTML.test(html)) {
    var loc = html.search(validXHTML);
    var str = html.match(validXHTML);
    html = html.substr(0, loc) + 
            strings.addPunctuation(html.substr(loc, str[0].length).replace(validXHTML, '$2')) + 
            html.substr((loc+str[0].length), html.length);

    if(validXHTML.test(html)) { 
        html = strings.stripHTML(html);
    } else {
        return html;
    }
   }
   return html;
}

Feel free to improve the above, or answer the actual question.


Update

My idea for a simple way to at least accommodate most cases is this:

encode all > and < not that close or open nothing,

change all tag-names inside < > to lowercase

working recursively, start with the inner-most tags, change them from lowercase to upper case <li>something</li> becomes <LI>something</LI>

after recursion finishes, strip out all other > and <

switch all uppercase tags back to lowercase

Are any problems immediately foreseen, other than the fact it will take a fair amount of time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:30:17+00:00Added an answer on May 11, 2026 at 8:30 pm

    I do this on the server with HTMLTidy

    htmltidy -asxhtml
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know a good way to solve this other problem I have. My
I am pretty familiar with CSS and have used quite a bit of javascript
I have some acceptance test allows a Windows service to send an email out
What I'm currently doing is using jQuery to perform an ajax request on every
I have a Flex 4/Blaze/Java application and would like to provide my users with
I have a webpage that takes 10 minutes to run one query against a
Throughout my days as a PHP Programmer and a beginner at C# programming I've
I am just getting to grips with the event scheduler in MySQL and I
I do start off organising my .h files with the best intentions but somehow
I am getting ready to submit an educational app to Apple for review. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.