6

I have a TextArea which allows the user to enter HTML, what I am now trying to do is to validate the users HTML to ensure it is XHTML.

Any ideas?

Coppermill
  • 6,508
  • 13
  • 65
  • 91

2 Answers2

4

You can use a DOM Parser to see if the content is XML.

See here.


SNIPPET:

if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(text,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(text);
  } 
Topera
  • 11,829
  • 14
  • 65
  • 101
  • var error =$(xmlDoc.documentElement).find('parsererror').first(); if(error) ... Something like this will tell you if there is an error and a not very helpful error as `error.text()` – ponchietto Feb 07 '16 at 14:45
0

I would suggest using regular expressions. Start at this post here

I havent seen a jquery plugin that does exactly what you want itself, but i'm sure you can alter some existing validation.

Community
  • 1
  • 1
Bob
  • 3,054
  • 11
  • 42
  • 61
  • 2
    *cough* http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Mottie Aug 25 '10 at 12:05