ka********@yahoo.com wrote:
<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.