HTML checker form

H Mark

Thanks for you reply.

I have pasted the entire code below. Basically if I enter “asd” into the text area and hit the submit query" button the results state “No tags found” when I would expect it to say that there is a missing tag on line 1.

If I then add the closing tag to my text and click on the “Submit query” button nothing happens. The message still says “No tags found”.

If I then clear the text altogether and then enter “asd” alone nothing happens. Again, I get the “No tags found” message. In this instance I would have expected to proceed to the “secondpage.asp” page.

I look forward to hearing from you.

Regards

Rod from the UK

<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript">

//<![CDATA[

  bkLib.onDomLoaded(function() {

        new nicEditor({buttonList : ['bold','italic','underline','left','center','right','justify','ol','ul','indent','outdent','hr','forecolor','fontSize','fontFamily','fontformat','xhtml']}).panelInstance('eventdescription');

        new nicEditor({maxHeight : 100}).panelInstance('eventdescription');

  });

  //]]>

  </script>


<form method="post" action="secondpage.asp">
  <textarea id="eventdescription" style="width:100%;height:300px" rows="1" cols="20">
  </textarea>
  <div><input type="submit" /></div>
  <br />
  <h3>Results</h3>
  <pre id="unclosed-tag-finder-results">
  </pre>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">

function isSelfClosingTag(tagName) {
  return tagName.match(/area|base|br|col|embed|hr|img|input|keygen|link|menuitem|meta|param|source|track|wbr|script/i);
}
$('form').submit(function (event) {
  var input = $('#eventdescription').val();
  var tags = [];
  $.each(input.split('\n'), function (i, line) {
    $.each(line.match(/<[^>]*[^/]>/g) || [], function (j, tag) {
      var matches = tag.match(/<\/?([a-z0-9]+)/i);
      if (matches) {
        tags.push({tag: tag, name: matches[1], line: i+1, closing: tag[1] == '/'});
      }
    });
  });
  if (tags.length == 0) {
    $('#unclosed-tag-finder-results').text('No tags found.');
    event.preventDefault();
    return;
  }
  var openTags = [];
  var error = false;
  var indent = 0;
  for (var i = 0; i < tags.length; i++) {
    var tag = tags[i];
    if (tag.closing) {
      var closingTag = tag;
      if (isSelfClosingTag(closingTag.name)) {
        continue;
      }
      if (openTags.length == 0) {
        $('#unclosed-tag-finder-results').text('Closing tag ' + closingTag.tag + ' on line ' + closingTag.line + ' does not have corresponding open tag.');
        event.preventDefault();
        return;
      }
      var openTag = openTags[openTags.length - 1];
      if (closingTag.name != openTag.name) {
        $('#unclosed-tag-finder-results').text('Closing tag ' + closingTag.tag + ' on line ' + closingTag.line + ' does not match open tag ' + openTag.tag + ' on line ' + openTag.line + '.');
        event.preventDefault();
        return;
      } else {
        openTags.pop();
      }
    } else {
      var openTag = tag;
      if (isSelfClosingTag(openTag.name)) {
        continue;
      }
      openTags.push(openTag);
    }
  }
  if (openTags.length > 0) {
    var openTag = openTags[openTags.length - 1];
    $('#unclosed-tag-finder-results').text('Open tag ' + openTag.tag + ' on line ' + openTag.line + ' does not have a corresponding closing tag.');
    event.preventDefault();
    return;
  }
  $('#unclosed-tag-finder-results').text('Success: No unclosed tags found.');
});
</script>