Archive for July, 2007

JavaScript to Display the Current Month

Posted in JavaScript, programming on July 26, 2007 by Joey

The current month can be determined in JavaScript by using the getMonth method of the JavaScript date object, as follows:

var dteNow = new Date();
document.write(dteNow.getMonth());

The value returned by the getMonth method is an integer value between 0 and 11, with 0 being January and 11 being December. (Note that the date is determined from the client machine date.) To convert this integer to a string representation of the month, use a switch statement:

var intMonth = dteNow.getMonth();
var strMonth = '';

switch (intMonth)
{
case 0: strMonth = 'January'; break;
case 1: strMonth = 'February'; break;
case 2: strMonth = 'March'; break;
case 3: strMonth = 'April'; break;
case 4: strMonth = 'May'; break;
case 5: strMonth = 'June'; break;
case 6: strMonth = 'July'; break;
case 7: strMonth = 'August'; break;
case 8: strMonth = 'September'; break;
case 9: strMonth = 'October'; break;
case 10: strMonth = 'November'; break;
case 11: strMonth = 'December'; break;
}

document.write(strMonth);

Related articles:
JavaScript Date Object
JavaScript to Get the Day of the Week
JavaScript to Get the Current Year
JavaScript Switch Case Statement Example

JavaScript String Object indexOf Method

Posted in JavaScript, programming on July 25, 2007 by Joey

The JavaScript string object contains an indexOf method. It returns the index in the string of the first occurrence of a specified character. The index is numbered starting at 0. For example:

var strTest = 'only one day';
document.write(strTest.indexOf('n'));

The index returned in the preceding example is 1, as 'n' is the second character in the string and string indexes are counted starting at 0.

The indexOf method accepts an optional second argument – the position at which to begin the search.

var strTest = 'only one day';
document.write(strTest.indexOf('n',2));

In this example the index returned is 6. Since the search started at the second character position, the 'n' that exists at character position 1 is skipped and the next 'n' is searched for, which exists at position 6.

JavaScript to Get the Current Year

Posted in JavaScript, programming on July 24, 2007 by Joey

The current year can be retrieved in JavaScript by using the getFullYear method of the Date object, as follows:

var dteNow = new Date();
var intYear = dteNow.getFullYear();
document.write(intYear);

JavaScript to Get the Day of the Week

Posted in JavaScript, programming on July 22, 2007 by Joey

The day of the week can be retrieved in JavaScript by using the getDay method JavaScript’s Date object, as follows:

var dteNow = new Date();
var dteToday = dteNow.getDay();
document.write(dteToday);

The value returned by getDay is between 0 (Sunday) and 6 (Saturday). To convert this to a string representing the day of the week, use a switch statement.

var strDay = '';

switch(dteToday)
{
case 0: strDay = 'Sunday'; break;
case 1: strDay = 'Monday'; break;
case 2: strDay = 'Tuesday'; break;
case 3: strDay = 'Wednesday'; break;
case 4: strDay = 'Thursday'; break;
case 5: strDay = 'Friday'; break;
case 6: strDay = 'Saturday'; break;
}

document.write(strDay);

Related articles:
JavaScript Date Object
JavaScript Switch Case Statement Example

JavaScript to Remove Dollars Signs From a Number

Posted in JavaScript, Regular Expressions, Software, programming on July 22, 2007 by Joey

A common problem with HTML input forms is that users tend to include dollars signs when inputting currency numbers. These dollars signs can be easily removed through JavaScript by using the JavaScript replace function and a simple regular expression. This is demonstrated in the following example:

var Amount = '$13.22';
var ReplacedAmount = Amount.replace(/\$/g,'');
document.write(ReplacedAmount);

This example shows a dollar amount that includes a dollar sign. To remove the dollar sign, use the replace function. The first argument to the replace function is a regular expression. This regular expression states that all dollar signs in the Amount variable be found. The replace function then replaces those dollars signs with nothing.

Related articles:
JavaScript Regular Expressions
JavaScript Regular Expression Backslash

Labeling HTML Form Elements

Posted in JavaScript, Software, internet, programming on July 22, 2007 by Joey

The HTML label tag is used to label form elements as follows:

<label for="FirstNameLabel">First Name:</label>
<input type="text" name="FirstName" id="FirstNameLabel">

As seen in this example, the for attribute of the label tag binds it to the id attribute of the input control. Why use the label tag? Browsers handle the text in a label tag in a unique way. When the label text is clicked on, focus is automatically given to the associated form control. This is especially useful in the case of radio buttons and checkboxes, as their text can be clicked to toggle the control. For example:

<input type="radio" name="OnOffControl" id="on">
<label for="on">On</label>
<input type="radio" name="OnOffControl" id="off">
<label for="off">Off</label>

Another compelling reason to use the label tag is that it aids browsers that are built for people with disabilities. An example is speech browsers, which use the label to determine how to describe the form controls.

Favorite Recent Links 07/20/2007

Posted in JavaScript, Software, internet, programming on July 20, 2007 by Joey

Three must-read JavaScript articles

101 Ways to Know Your Software Project is Doomed

My favorites:
You start considering a new job so you don’t have to maintain the application you are building
Developers use the excuse of ’self documenting code’ for no comments
You believe compiling is a form of testing
Your manager wastes 7 hours a week asking for progress reports
Team Rule – No meetings until 10 AM since we were all here until 2 AM
The phrase ‘It works on my machine’ is heard more than once a day
Developers are not responsible for any testing

How Popular Sites Looked in the 1990s

JavaScript Is Not a Number Function: isNaN()

Posted in JavaScript, Software, programming on July 20, 2007 by Joey

The JavaScript isNaN function accepts one argument and returns true if that argument is not a number value and false if the argument is a number value. For example:

document.write(isNaN(10))//Is a number, returns false
document.write(isNaN('a'))//Is not a number, returns true

JavaScript Regular Expression Caret ^

Posted in JavaScript, Regular Expressions, Software, Web, internet, programming on July 17, 2007 by Joey

In JavaScript regular expressions, the caret (^) special character can be used in two ways.

First, it can be used as a line anchor, specifying the beginning of a line. I explained this previously in my post JavaScript Regular Expression Special Characters.

Second, it can be used inside a character class to negate the characters being searched for in the character class. Please see my post JavaScript Regular Expression Square Bracket Negation.

Related articles:
JavaScript Regular Expressions

JavaScript Regular Expression Hyphen, or Dash

Posted in JavaScript, Regular Expressions, Software, Web, internet, programming on July 16, 2007 by Joey

In a JavaScript regular expression the hyphen (also commonly referred to as the dash) can serve two purposes, depending upon where it is used in the expression.

First, a hyphen can be used as part of a regular expression if the hyphen needs to be found literally. An example of this is a regular expression that looks for a phone number match, such as:

/^d{3}-\d{3}-\d{4}$/

In the preceding example, the hyphen is searched for literally as a separator in the phone number.

A hyphen can also be used as part of a character class to define a range of characters. Expanding upon the phone number regular expression above as an example:

/^[2-9]\d{2}-\d{3}-\d{4}$/

This regular expression has now been updated to specify that the first character must be a number in the 2 to 9 range. Another common use of the hyphen to define a range of characters is:

[A-Za-z0-9] which matches any letter or number character.

It should also be noted that even within a character class the hyphen is only used as a range character if it is between two characters that define as range. That is, if a character class has a hyphen in it that does not define a range, it is then treated as a literal hyphen, just as it would be outside a character class.

Related articles:
JavaScript Regular Expressions