PDA

View Full Version : learning javascript - i have a question


s_dragons_lair_c
06-20-2007, 08:18 AM
I dont know if this is the right place to put this question but i couldnt find any other place to put it either.

So Im learning javascript from a book and the following is one of its examples.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html dir="ltr" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Date example</title>
<style type="text/css">
.error{color:#c00;font-weight:bold;}
</style>
<script type="text/javascript">
function checkDate(){
if(!document.getElementById || !document.createTextNode){return;}
var dateField=document.getElementById('date');
if(!dateField){return;}
var errorContainer=dateField.parentNode.getElementsByT agName('span')[0];
if(!errorContainer){return;}
var checkPattern=new RegExp("\\d{2}/\\d{2}/\\d{4}");
var errorMessage='';
errorContainer.firstChild.nodeValue=' ';
var dateValue=dateField.value;
if(dateValue==''){
errorMessage='Please provide a date.';
} else if(!checkPattern.test(dateValue)){
errorMessage='Please provide the date in the defined format.';
}
if(errorMessage!=''){
errorContainer.firstChild.nodeValue=errorMessage;
dateField.focus();
return false;
}else{
return true;
}
}
</script>
</head>
<body>
<h1>Events search</h1>
<form action="eventssearch.php" method="post" onsubmit="return checkDate();">
<p>
<label for="date">Date in the format DD/MM/YYYY:</label><br />
<input type="text" id="date" name="date" />
<input type="submit" value="Check " />
<br />(example 26/04/1975) <span class="error"> </span>
</p>
</form>
</body>
</html>

The only problem is it doesnt actually give the error message. But I did find out that a small change does make the script to give the error message.
I changed the text between the span tags to &nbsp; and it started working.

But i cant figure out why that happened.

davidj
06-20-2007, 09:29 AM
thats a strange one

i use the innerHTML method which has always been good for me

s_dragons_lair_c
06-21-2007, 02:10 AM
sry but im not familiar with that method.
but im still curious to know abt my original prob.
so ur saying that it's not supposed to happen.
or maybe its because of the browsers i use. IE7 & Opera 9.21.