Question

Locked

Validating XHTML with javascript in the code

By lynda ·
Hello, all. Although I've been creating web pages for nearly 15 years, I've only recently stopped being lazy and started making sure I write valid, compliant code. Long story - don't ask.

It's very possible that this is obvious to everyone but me. However, I decided a long time ago that there are no stupid questions, so here goes.

http://validator.w3.org/ has become my best friend/worst enemy. Now I'm finding that the validator is callikng javascript code within <script></script> tags invalid XHTML code, which of course keeps my pages from validating. For example:


# Line 72, Column 33: character "<" is the first character of a delimiter but occurred as data.

    } else if ((fld.value.length < 1) || (fld.value.length > 10)) {

✉

This message may appear in several cases:

    * You tried to include the "<" character in your page: you should escape it as "<"
    * You used an unescaped ampersand "&": this may be valid in some contexts, but it is recommended to use "&", which is always safe.
    * Another possibility is that you forgot to close quotes in a previous tag.

# Error Line 72, Column 34: XML Parsing Error: StartTag: invalid element name.

    } else if ((fld.value.length < 1) || (fld.value.length > 10)) {


This is the only warning, and the only error, on the page.


The entire code block is

<script type="text/javascript">
function validateFormOnSubmit(theForm) {
var reason = "";

  reason += validateUsername(theForm.username);
  if (reason != "") {
    alert("User fields need correction:\n" + reason);
    return false;
  }

  return true;
}


function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 1) || (fld.value.length > 10)) {
        fld.style.background = 'Yellow'; 
        error = "The username is too long.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}
</script>


I didn't write the code, I'm validating a site created by someone else who isn't available right now, and I'm pretty sure he wouldn't have the answer, anyway. I don't know Javascript, although I understand programming logic and have a vague idea of what this does.

All the paired elements - parens, quotes, braces, and the <style> tags - seem to be correct, so what am I missing? Why is the W3C validator calling it invalid XHTML? Can I fix it?

This conversation is currently closed to new comments.

2 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Answers

Share your knowledge
Back to Web Development Forum
2 total posts (Page 1 of 1)  

Related Discussions

Related Forums