Forum Moderators: open

Message Too Old, No Replies

Javascript not validating as Transitional

         

PartisanEntity

1:16 pm on Oct 19, 2008 (gmt 0)

10+ Year Member



Hi all,

I am a student of Multimedia Arts and currently we are working our way through Javascript. I have a test coming up soon and I know that one of the questions will be to create a username and email form, with Javascript checking the fields.

I have been practicing and the script works quite nicely, but what I don't understand, is why it is not validating in DTD Transitional?

Any help would be greatly appreciated.

My Javascript in the head:


<script type="text/javascript">
function Efunc() {
var Email = document.getElementById("email").value;
var UName = document.getElementById("name").value;
var UName_length = UName.length;
var Email_at = Email.indexOf("@");
var Email_dot = Email.indexOf(".");
var Email_length = Email.length;
if (Email_at != -1 &&
Email_at >= 1 &&
Email_dot != -1 &&
Email_dot >= Email_at + 2 &&
Email_dot <= Email_length-3 &&
UName_length > 1 ) {
alert("Korrekt");
} else {
alert("Falsch");
}
}
</script>

The form in the body:


<form id="emailform" onsubmit="Efunc()" action="EmailCheck2.html">
<p>Email: <input type="text" id="email" /></p>
<p>Name: <input type="text" id="name" /></p>
<input type="submit" />
</form>

birdbrain

4:54 pm on Oct 19, 2008 (gmt 0)



Hi there PartisanEntity,

and a warm welcome to these forums. ;)

I assume from a glance at your code that you are using an xhtml transitional dtd.


You're better off not pretending that this is XHTML, really.
If you're serving it as "text/html" then it is, for all practical purposes, HTML, not XHTML.
Not pretending the document is XHTML and instead using HTML4.01 makes the document smaller,
with no ill effects at all.
HTML4.01 and XHTML1.0 are practically identical in what elements they allow and what their element
content models are, but they are not compatible in some ways with regards to how empty elements are
handled, with regards to style sheets, with regard to the DOM, and with regards to stuff like CDATA blocks
and with regards to case sensitivity.



An html transitional dtd will validate OK. ;)

If you have your heart set on using an xhtml transitional dtd, then wrap the the script contents in CDATA blocks...


<script type="text/javascript">
[red]//<![CDATA[[/red]
function Efunc() {
var Email = document.getElementById("email").value;
var UName = document.getElementById("name").value;
var UName_length = UName.length;
var Email_at = Email.indexOf("@");
var Email_dot = Email.indexOf(".");
var Email_length = Email.length;
if (Email_at != -1 &&
Email_at >= 1 &&
Email_dot != -1 &&
Email_dot >= Email_at + 2 &&
Email_dot <= Email_length-3 &&
UName_length > 1 ) {
alert("Korrekt");
} else {
alert("Falsch");
}
}[red]
//]]>[/red]
</script>

...or better still put the javascript in an external file.

birdbrain

PartisanEntity

7:47 am on Oct 20, 2008 (gmt 0)

10+ Year Member



Thanks very much!

birdbrain

10:34 am on Oct 20, 2008 (gmt 0)



No problem, you're very welcome. ;)