473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

HTML Validation

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.

Jul 23 '05 #1
5 1491


Very simple to do really, just use a RegExp:
var isValid=/^<[\w-\s="\/]+\/*>$/; //then
if (isValid.test(YOURSTRINGHERE)) or so, OR

you could check at http://www.javascriptkit.com/cutpastejava.shtml for
some premades ones.
Danny

On Tue, 12 Jul 2005 20:13:54 -0700, <ka********@yahoo.com> wrote:
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.


--
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/
Jul 23 '05 #2
<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.

Jul 23 '05 #3
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.

Jul 23 '05 #4
ka********@yahoo.com wrote:
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.
Jul 23 '05 #5


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

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dfirefire | last post by:
Hi, i tried to validate a html doc, but the validator complained without reason. This is an excerpt of the validation results: Below are the results of attempting to parse this document with an...
81
by: sinister | last post by:
I wanted to spiff up my overly spartan homepage, and started using some CSS templates I found on a couple of weblogs. It looks fine in my browser (IE 6.0), but it doesn't print right. I tested...
15
by: Pasta Bolognese | last post by:
Open VS.2003. Click on a control on an aspx page in the HTML view. Does it tell me the pixel position on the page? Nooooooooooo.................
2
by: Nick Gilbert | last post by:
Hi I have a number of pages where it is valid for the user to enter HTML. On these pages, I have turned off RequestValidation ("ValidateRequest = false" in the page directive) so that the...
9
by: Alan Silver | last post by:
Hello, I am writing a web form where the user is allowed to enter HTML into one of the textboxes. This of course generates the "potentially dangerous" server error. Now I know I can switch...
4
by: Arthur Dent | last post by:
Hello all, ive been programming with ASP.NET since it came out, but am just getting my feet with now with v.2. Ive noticed something strange in the way my HTML tables get rendered with 2. I use...
12
by: Nalaka | last post by:
Hi, I suddenly started getting a lot of errors from html validation (some CSS) so I followed the following instructions to disable it. If you'd rather not have these types of HTML validation...
40
by: VK | last post by:
Hi, After the response on my request from W3C I'm still unclear about Tidy vs. Validator discrepansies. That started with <IFRAME> issue, but there is more as I know. Anyway, this very basic...
1
by: Tatyana | last post by:
Hi All, I have a question regarding validation with XHTML on ASP.Net websites. Our site is built on ASP.NET 2.0 with validation XHTML. Search Engine Optimizer advised us that we should change it...
8
by: =?Utf-8?B?U2hhd24gUmFtaXJleg==?= | last post by:
I am coming at the world from the standpoint of a veteran ASP Classic developer. I am not clear on the advantages of using the ASP.NET control over standard HTML control. With the ASP.NET...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.