I need a reliable JavaScript library / function to check if an HTML snippet is valid that I can call from my code. For example, it should check that opened tags and quotation marks are closed, nesting is correct, etc.
I don't want the validation to fail because something is not 100% standard (but would work anyway).
Take checkHTML("<p>Test<P>test") for instance. That is perfectly valid HTML, but the browser will normalize it when it pulls it back out of innerHTML . There will be no text outside an element in the DOM generated from that valid HTML.
test with "<p>fizz buzz</p>" to check if "<p>fizz buzz</p>" is a JavaScript string. We use <\/?[a-z][\s\S]*> to check for any tags in the string to check if it has any HTML markup.
The valid HTML tag must satisfy the following conditions: It should start with an opening tag (<). It should be followed by a double quotes string or single quotes string. It should not allow one double quotes string, one single quotes string or a closing tag (>) without single or double quotes enclosed.
Update: this answer is limited - please see the edit below.
Expanding on @kolink's answer, I use:
var checkHTML = function(html) { var doc = document.createElement('div'); doc.innerHTML = html; return ( doc.innerHTML === html ); }
I.e., we create a temporary div with the HTML. In order to do this, the browser will create a DOM tree based on the HTML string, which may involve closing tags etc.
Comparing the div's HTML contents with the original HTML will tell us if the browser needed to change anything.
checkHTML('<a>hell<b>o</b>')
Returns false.
checkHTML('<a>hell<b>o</b></a>')
Returns true.
Edit: As @Quentin notes below, this is excessively strict for a variety of reasons: browsers will often fix omitted closing tags, even if closing tags are optional for that tag. Eg:
<p>one para <p>second para
...is considered valid (since Ps are allowed to omit closing tags) but checkHTML
will return false. Browsers will also normalise tag cases, and alter white space. You should be aware of these limits when deciding to use this approach.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With