Validation Problem

Discussion in 'JavaScript' started by Masterful, Mar 26, 2009.

  1. #1
    I use the script below on one of my pages. It limits the number of characters a user can enter into my text area.

    Script:

    <script type="text/javascript">
    function textCounter(field,cntfield,maxlimit)
    {
       if (field.value.length > maxlimit) // If too long, trim it.
          field.value = field.value.substring(0, maxlimit);
       // Otherwise, update 'characters left' counter
       else
          cntfield.value = maxlimit - field.value.length;
    }
    </script>
    Code (markup):
    HTML:

    <form method="post" name="myForm" action="process.php">
    
    <textarea name="prod" cols="72" rows="3"
    onkeydown="textCounter(document.myForm.prod_serv,document.myForm.remLen1,255)"
    onkeyup="textCounter(document.myForm.prod_serv,document.myForm.remLen1,255)"></textarea>
    <br />
    <input readonly="readonly" type="text" name="remLen1" size="3" maxlength="3" value="255" />
    characters left
    
    </form>
    Code (markup):
    The script works fine. However, the HTML does not validate.

    My Doctype is XHTML 1.0 Strict. Apparently, the 'name' attribute cannot be used in the <form> tag. It's been deprecated for id. However, when I change it to id, the script doesn't work. Anyone know how I can fix this?

    My bookmark script also generates validation errors:

    function bookmarksite(title,url)
    {
       if (window.sidebar) // firefox
          window.sidebar.addPanel(title, url, "");
       else if(window.opera [b]&&[/b] window.print){ // opera
          var elem = document.createElement('a');
          elem.setAttribute('href',url);
          elem.setAttribute('title',title);
          elem.setAttribute('rel','sidebar');
          elem.click();
    }
       else if(document.all)// ie
          window.external.AddFavorite(url, title);
    }
    Code (markup):
    The two ampersands (&) are the problem. First, a warning is given for each one: "character '&amp;' is the first character of a delimiter but occurred as data". Then, an error is given: "XML Parsing Error: xmlParseEntityRef: no name".

    Anyone know how I can clean this up so it'll validate?
     
    Masterful, Mar 26, 2009 IP
  2. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #2
    Guys, I have fixed my first problem. Now, just this problem remains:

    function bookmarksite(title,url)
    {
       if (window.sidebar) // firefox
          window.sidebar.addPanel(title, url, "");
       else if(window.opera [B]&&[/B] window.print){ // opera
          var elem = document.createElement('a');
          elem.setAttribute('href',url);
          elem.setAttribute('title',title);
          elem.setAttribute('rel','sidebar');
          elem.click();
    }
       else if(document.all)// ie
          window.external.AddFavorite(url, title);
    }
    Code (markup):
    Those damn ampersands (&) won't validate. Anyone know how I can get rid of them without messing up the script?
     
    Masterful, Mar 26, 2009 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    I no longer need assistance with this matter, guys.

    The solution was to wrap the script in CDATA tags. The tags hide the script from the XML parser, which is what was throwing the errors whenever it encountered the ampersands. The problem is, when you use CDATA tags, the Javascript doesn't work, which is why you also have to comment out the CDATA tags, thus:

    <script type="text/javascript">
    
    [B]//<![CDATA[ [/B]
    
    function bookmarksite(title,url)
    {
    	if (window.sidebar) // firefox
    		window.sidebar.addPanel(title, url, "");
    	else if(window.opera && window.print){ // opera
    		var elem = document.createElement('a');
    		elem.setAttribute('href',url);
    		elem.setAttribute('title',title);
    		elem.setAttribute('rel','sidebar');
    		elem.click();
    } 
    	else if(document.all)// ie
    		window.external.AddFavorite(url, title);
    }
    
    [B]//]]>[/B]
    
    </script>
    Code (markup):
    Use this method whenever Javascript gives you validation problems.
     
    Masterful, Mar 27, 2009 IP