View Single Post
Old 12-08-2012, 02:40 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,200
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
The only place you have defined addDueDate is in this code:
Code:
function addList () {
    var addDueDate=document.list.datedue.value;
    var addToList=document.list.todo.value;
    document.getElementById("todolist").innerHTML= "<table><tr><td> Due Date </td><td>Task</td></tr><tr><td>" + addDueDate + "</td><td> " + addToList +"</td></tr></table>";
}
But that variable is FUNCTION SCOPE, meaning that as soon as the addList() function is finished running, that variable is gone into the ether. Zap. Non-existent.

You could move the variable to global scope, but I think that's a bad idea.

A little extra code isn't going to hurt, especially if it keeps intent clear:
Code:
function validDate() {
    var re= /^\d{2}[.\/-]\d{2[.\/-]\d{4}$/;
    if (re.test(document.list.datedue.value)== false) {
	alert("please enter a valid date");
    }
}
NOW...there's another problem: You don't DO this validation until *AFTER* the item has ALREADY been "published" in the <table>. (And, by the by, it's bad practice to use innerHTML to generate an entier <table>, most experts would say.)

So wouldn't it make more sense to simply incorporate the test into the the addList( ) function and then *NOT* publish to the <table> if the date is invalid?

*NOW* you could simply do:
Code:
function addList () {
    var addDueDate=document.list.datedue.value;
    var re= /^\d{2}[.\/-]\d{2[.\/-]\d{4}$/;
    if ( ! re.test(addDueDate) ) {
	alert("please enter a valid date");
        return;
    }
    var addToList=document.list.todo.value;
    document.getElementById("todolist").innerHTML= "<table><tr><td> Due Date </td><td>Task</td></tr><tr><td>" + addDueDate + "</td><td> " + addToList +"</td></tr></table>";
}
And get rid of your validDate() function and the call to it.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote