Forum Moderators: open
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>
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.
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>
birdbrain
No problem, you're very welcome. ;)