New Posts

Wednesday, May 18, 2011

Common Validation Errors

In a previous post i talked about the importance of validation, but I never delved into what types of errors you would commonly encounter. Today we are going to take a look at 3 common errors you encounter when validating.


There is no attribute "xxxx"



Html elements or tags have attributes one can assign to them. things like a name, and ID, a class but sometimes we may try to use an attribute that an element doesn't have. Or an attribute it may have in one particular doctype but not in another.

For instance lets say we are building a form to get user input:

From Code

<form name="MyForm" action="mypage.php" method="POST">

<p>User:<input type="text" name="user"></p>

<p>Password:<input type="password" name="user"></p>

...

</form>



If I were using an XHTML strict doctype in the website that contained this, the error would be something like:


Line 7, Column 12: there is no attribute "name"
<form name="MyForm" action="mypage.php" method="POST">
You have used the attribute named above in your document, but the document type you are using does not support that attribute for this element.


In <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"

"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> only input elements can have a name. Divs,paragraphs, links etc.. cannot have a name attribute.

Most would think a name is vital to be able to access form elements if you are using JS to validate the form. For XHTML Strict, you only need an ID for the element you wish to access through Javascript.


End tag for "xxxx" omitted, but OMITTAG NO was specified



This one of the more common mistakes simply because its the easiest to over look.

It happens on certain tags such as image, break and any other tag that doesn't require a closing tag to accompany it unlike other tags like divs, links, spans etc...

Closing Tags


<div></div>

<a></a>

<span></span>



----



Self Closing


<img src="">



<br>



<input type="text">




Self closing tags like above require an extra special character to properly close them. the forward slash. In XHTML strict these tags need to be terminated as .../> to be properly closed, otherwise they generate errors. Since Transitional doctypes don't require this, its easy to forget to add them at the end.



document type does not allow element "xxx" here; missing one of a, b, c,d,e,...



This error is more of a nitpicky error, Basically what is says is that whatever xxx is, it cannot be placed wherever it is you have it because it requires something else to contain it. The most common example of this are label tags. Most would assume you can simply add them in front of an input to add a text label to it. XHTML Strict doctypes will barf at it as they require the label to be inside a block level element such as a div or a paragraph.

Wrong


<form ...>

<label>My Label</label><input type="text">

</form>



Right


<form ...>

<p><label>My Label</label><input type="text"></p>

</form>





There are many more errors, some harder to understand than others, however its important to read the provided reasons for the error, as more often than not they contain vital clues to what may be wrong with that particular line.

No comments:

Post a Comment