HTML Validation

K

karl_lopes

Hi,
I was looking for any validation code in javascript which
essentially does the same thing as html tidy. i.e. The user enters some
data in a form text area and I want to perform a client side data
validation to ensure that it is valid xhtml before I submit the form.
Any ideas?
Karl.
 
K

karl_lopes

<html>
<head>
<script>
function isValid(){
var isValid=/^<[\w-\s="\/]+\/*>$/;
if(isValid.test("<b>Hello World</b>")){
alert("It is Valid");
}else{
alert("Invalid");
}
}
</script>
</head>

<body onload='isValid();'>
CCC
</body>


</html>
fails!! It should pass. I am not looking for a regular expression
solution I am looking for a solution which ensures that the tags are
valid XHTML tags i.e. <b>XXX</b> should pass but <ccc>Fail</ccc> should
fail.
 
C

Christopher J. Hahn

<html>
<head>
<script>
function isValid(){
var isValid=/^<[\w-\s="\/]+\/*>$/;
if(isValid.test("<b>Hello World</b>")){
alert("It is Valid");
}else{
alert("Invalid");
}
}
</script>
</head>

<body onload='isValid();'>
CCC
</body>


</html>
fails!! It should pass. I am not looking for a regular expression
solution I am looking for a solution which ensures that the tags are
valid XHTML tags i.e. <b>XXX</b> should pass but <ccc>Fail</ccc> should
fail.

The RegExp is flawed, but clearly it wouldn't do what you're after if
it weren't.

There're several ways to do what you're looking for.

One suggestion:
Create an object and populate it with all possible XHTML tags you want
to consider valid.

var validTag = {
a: 1,
b: 1,
i: 1,
link: 1,
script: 1
};

ad nauseum. I'm not familiar with XHTML or whether it supports
namespacing, but if so, you can do some parsing of the fragment to
obtain those namespaces and check accordingly.

Having done that, you can now easily use a RegExp to extract tagnames
from any HTML or XHTML fragment and quickly check whether each tagname
is also a member of your validTag object.





Does XHTML not allow for custom tags?
i.e. In both FF and IE, working in HTML (purely quirksy), I can create
a node by an arbitrary name (number, ticket, ccc, or whatever) and work
with it as with any other HTMLElement. It's incredibly useful.
 
R

Robert

Hi,
I was looking for any validation code in javascript which
essentially does the same thing as html tidy. i.e. The user enters some
data in a form text area and I want to perform a client side data
validation to ensure that it is valid xhtml before I submit the form.
Any ideas?
Karl.

You would need to use a client side XML parser and use the XML schema
for XHTML Strict (1.0) to validate it.
 
M

Martin Honnen

The user enters some
data in a form text area and I want to perform a client side data
validation to ensure that it is valid xhtml before I submit the form.

Then you need to use a validating XML parser to parse the data. MSXML
which comes with IE on Windows is a validating parser so it could help
you doing that, in theory at least. In reality the problem is that MSXML
is not going to load the DTD from w3.org if you script is loaded from
another server, unless you change the security settings.
It could work if you place the DTDs (or the DTD you want to validate
against) on your own server and then make sure that e.g.
http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd in the DOCTYPE
declaration is replaced by the URL to the resource on your server but I
have never tried that.
And other browsers like Mozilla or Opera do not have a validating XML
parser so you are better off doing the validation on the server.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top