
Originally Posted by
SunKiss
It goes without saying that I'm very much in the learning stages of JS. The biggest problem I've had is finding examples that explain adaquately how to deal with form input, etc.
You should start by reading the comp.lang.javascript FAQ. Entry 4.13 discusses the basics of accessing form controls, and the related FAQ notes (linked at the end of the entry) goes into into more detail.
As for validating user input, you really need to get to grips with regular expressions. If you're not familiar with them, they're a powerful pattern matching tool, though quite intimidating at first. All I can really recommend is that you find a good tutorial and then practice. You can also ask here for help, of course. 
and the logic and syntax for coding.
There's no simple solution to that. Having programming experience in any C-like language (including Java) will help as the syntax and control structures are quite similar. However, ECMAScript derivatives like JavaScript are also quite unique, and it takes time and effort to learn the language properly.
You might begin with the JavaScript 1.5 Guide. I started with the earlier (and now out-of-date) 1.3 version. Be aware that this is oriented towards the language as implemented by Mozilla, so there will be some features that aren't found in other browser implementations, but it should help with understanding the basics. Also, note that the object model (the features exposed by the browser) are documented separately, but one thing at a time.
After that, the best source of information, in my opinion, is the comp.lang.javascript newsgroup (your ISP might provide Usenet access, but there are also some free providers around). Usenet isn't always a friendly place[1], but perceived abuse isn't personal: it's usually apathy at answering the same question for the millionth time. At the time when I still misunderstood the language, I found myself on the recieving end of some very blunt corrections, but they proved to be lessons that I never forgot (which was the point). If you really want to understand things, that's the place to learn.
What braces in function did I leave out?
The ones that should surround the function body:
Code:
function CalcNow() {
/* ... */
}
Like in other languages, braces that form blocks (such as those that follow statements like if and for) can be omitted if that block contains only one statement:
Code:
if (condition) {
someFunction();
}
can also be written as:
Code:
if (condition)
someFunction();
However, sometimes they are required. There's no harm in always using them.
Can you give an example of the formal argument? How should it be in the form and function?
A formal argument is simply a named argument in the function declaration. For example,
Code:
function f(x, y) {
}
the function f has two formal arguments: x, and y.
You said:
Can you give an example or further explain how you would deal with this? Perhaps some code or reference to a code snippet?
What were you referring to there?
Do you mean that statements like:
Code:
var today = new Date()
should end with a semicolon like this?
Code:
var today = new Date();
Yes.
Code:
function parseDate(string, preferMDY) {
var match, year, month, date, result;
if ((match = /^(\d{4})([.\/-])(\d{1,2})\2(\d{1,2})$/.exec(string))) {
year = +match[1];
month = +match[3];
date = +match[4];
} else if ((match = /^(\d{1,2})([.\/-])(\d{1,2})\2(\d{4})$/.exec(string))) {
year = +match[4];
if (preferMDY) {
month = +match[1];
date = +match[3];
} else {
month = +match[3];
date = +match[1];
}
if (month > 12) {
var temp = month;
month = date;
date = temp;
}
}
with ((result = new Date(year, --month, date)))
if ((month == getMonth()) && (date == getDate())) return result;
return null;
}
function computeDayDifference(from, to) {
from = new Date(+from);
from.setHours(0, 0, 0, 0);
to = new Date(+to);
to.setHours(0, 0, 0, 0);
return Math.round((to - from) / 864e5);
}
Two functions are defined above: parseDate and computeDayDifference.
The first, parseDate, takes two arguments: a string containing a formatted date, and a boolean that helps determine how to handle dates in an ambiguous (##-##-####) format. Once parsed, the date is validated and either a new Date object is returned, or null.
The preferred form for the date is yyyy-mm-dd (though the hyphens can be replaced by dots [.] or slashes [/]) as this is the international date format, and easily recognised. If the format used is dd-mm-yyyy or mm-dd-yyyy, the function will try to make a simple guess as to which one is meant. If it cannot (both the two digit numbers are less than 12), it will assume that the month is first if the second argument is true, or the date is first if the argument is false or omitted. For example,
Code:
parseDate('19/09/2007', true)
the second argument would normally have the function treat that sort of string as mm-dd-yyyy, but because the supposed "month" is greater than 12, it will assume otherwise.
The second function, computeDayDifference, takes two arguments, both of which are dates (Date objects) in local time. The return value is the integer number of days difference between the two; a positive number indicates that the second date (to) follows the former (from).
Code:
function performCalculation(form) {
var controls = form.elements,
target = parseDate(controls.date.value, true); // Prefer mm-dd-yyyy
controls.days.value = target
? computeDayDifference(new Date(), target) + ' days'
: 'Invalid date format!';
}
HTML Code:
<form action="">
<fieldset>
<legend>Days from today</legend>
<label>Enter date (yyyy-mm-dd)</label>:
<input name="date" type="text" value="">
<input type="button" value="Calculate" onclick="performCalculation(this.form);"><br>
<input name="days" type="text" value="" disabled>
</fieldset>
</form>
Hope that helps,
Mike
[1] Please keep in mind that any hostility you might see directed at a poster that goes by the name VK, including any from yours truly, is deserved. VK is the group's resident idiot who has a tendency to post misleading information (though he believes himself to be correct). Ignore anything he writes unless it is corroborated by someone else.
Bookmarks