Archive for April, 2007

JavaScript Phone Number Validation

Posted in JavaScript, Software, Web, internet, programming on April 30, 2007 by Joey

A hyphen-separated United States phone number can be validated in JavaScript using the following regular expression:

(/^[2-9]\d{2}-\d{3}-\d{4}$/.test('222-333-4444');

Broken down into sections:

^[2-9] The phone number string must begin (^) with a digit between 2 and 9. This is the start of the area code. An area code cannot begin with 0 or 1.
\d{2} The first digit of the area code must be followed by two additional digits.
- The area code must be followed by a hyphen.
d{3} The hyphen after the area code must be followed by three digits. This is the local number prefix.
- The local number prefix must be followed by a hyphen
d{4}$ The local number prefix must be followed by four additional digits and this must end the phone number string ($)

Related Articles:
JavaScript Regular Expressions
JavaScript Regular Expression Special Characters: $ ^
JavaScript Number Validation (Repetition)
JavaScript Regular Expression Backslash
JavaScript Regular Expression Square Brackets

JavaScript pop Method: Removing From an Array End

Posted in JavaScript, Software, Web, internet, programming on April 30, 2007 by Joey

The JavaScript pop method can be used to remove elements from a JavaScript array.

//Removes 4 from the array
var aryTest = [1,2,3,4];
aryTest.pop();

JavaScript push Method: Adding to an Array End

Posted in JavaScript, Software, Web, internet, programming on April 29, 2007 by Joey

The JavaScript push method can be used to add elements to an array. Any values provided to the push function are appended to the end of the array.

//Adds 5 at the end of the array
var aryTest = [1,2,3,4];
aryTest.push(5);
document.write(aryTest);

Related Articles:
JavaScript unshift Method – Adding to an Array Beginning

JavaScript Array length Property

Posted in JavaScript, Software, Web, internet, programming on April 28, 2007 by Joey

The number of elements in a JavaScript array can be retrieved by using the length property of the Array object. Note that this includes any undefined elements.

//Returns 4
var aryTest = [1,2,3,4];
document.write(aryTest.length);

If an element is then added at the 5th position of the array (JavaScript arrays are seeded at 0) the length of the array is 6, even though the 4th position is undefined.

//Returns 6
var aryTest = [1,2,3,4];
aryTest[5] = 5;
document.write(aryTest.length);

So the individual array elements are now:
aryTest[0] = 1
aryTest[1] = 2
aryTest[2] = 3
aryTest[3] = 4
aryTest[4] = undefined
aryTest[5] = 5

From this we can see that the JavaScript array has a length of 6.

The length property of the JavaScript array object can also be used to expand and contract the array.

//Returns 10
var aryTest = [1,2,3,4];
aryTest.length = 10;
document.write(aryTest.length);

The preceding array was initialized to have four elements, then the length property was explicitly set to have 10 elements, which adds six undefined elements to the array.

The length of the array can be similarly contracted.

//Returns 3
var aryTest = [1,2,3,4];
aryTest.length = 3;
document.write(aryTest.length);

In the preceding example, the array is initialized with four elements. When the length is explicitly set to three the fourth element is truncated from the array.

Creating a JavaScript Array

Posted in JavaScript, Software, Web, internet, programming on April 27, 2007 by Joey

An array can be created in JavaScript by any of the following:

var aryTest = new Array();
This initializes an empty array

var aryTest = new Array(2);
This initializes an array with the number of elements indicated in the parenthesis. Each element is undefined, which is demonstrated by looping over the array as follows:
for(i=0;i<aryTest.length;i++)
alert(aryTest[i]);

var aryTest = new Array(1,2,3,4);
This initializes an array while populating it with values. Therefore, the length of this array would be four with 1 being the value of the first array element, 2 the value of the second array element and so forth.

JavaScript Zip Code Validation

Posted in JavaScript, Software, Web, internet, programming on April 26, 2007 by Joey

A 5-digit United States Zip Code can be validated in JavaScript with a regular expression:

/^\d{5}$/.test('84601')

This specifies that the string being tested must begin and end as a digit with a length of five.

JavaScript Email Address Validation

Posted in JavaScript, Software, Web, internet, programming on April 26, 2007 by Joey

Email addresses can be validated in JavaScript by using a regular expression:

(The following regular expression is actually one line – I have broken it into two so that it will wrap properly in WordPress.)

/^[A-Za-z0-9]+\w*([._-][a-zA-Z0-9]+)*@[A-Za-z0-9]+([._-][a-zA-Z0-9]+)*
\.[a-zA-Z]{2,}$/.test('joey@joey.com') ;

The JavaScript email address validation demonstrated here is somewhat loose to allow for varying email possibilities.

Broken down into sections:

^[A-Za-z0-9]+ – Begin with one or more letter or number characters
\w* – Followed by 0 or more ASCII word characters
([._-][a-zA-Z0-9]+)* – Followed by 0 or more occurrences of an optional period, underscore or hyphen, followed by one or more letter or number characters
@ – Followed by a mandatory alpha character
Domain validation starts here:
[A-Za-z0-9]+ – Followed by one or more letter number characters
([._-][a-zA-Z0-9]+)* – Followed by 0 or more occurrences of an optional period, underscore or hyphen, followed by one or more letter or number characters
\. – Followed by a mandatory period (must have something like .com, .org, .edu, etc.)
[a-zA-Z]{2,}$ – Ended by 2 or more letter characters

JavaScript Regular Expression Square Bracket Negation

Posted in JavaScript, Software, Web, internet, programming on April 26, 2007 by Joey

In JavaScript Regular Expressions the caret (^) can be used in square brackets as a negation character.

/[^abc]/ – would match for any characters that are not a, b or c.

/[^hb]at/ – would match for cat but not for hat or bat.

JavaScript Difference Between null and undefined

Posted in JavaScript, Software, Web, internet, programming on April 25, 2007 by Joey

In JavaScript, undefined means a variable has been declared but has not yet been assigned a value, such as:

var TestVar;
alert(TestVar); //shows undefined
alert(typeof TestVar); //shows undefined

null is an assignment value. It can be assigned to a variable as a representation of no value:

var TestVar = null;
alert(TestVar); //shows null
alert(typeof TestVar); //shows object

From the preceding examples, it is clear that undefined and null are two distinct types: undefined is a type itself (undefined) while null is an object.

Unassigned variables are initialized by JavaScript with a default value of undefined.

JavaScript never sets a value to null. That must be done programmatically. As such, null can be a useful debugging tool. If a variable is null, it was set in the program, not by JavaScript.

null values are evaluated as follows when used in these contexts:

Boolean: false
Numeric: 0
String: “null”

undefined values are evaluated as follows when used in these contexts:

Boolean: false
Numeric: NaN
String: “undefined”

JavaScript Regular Expression OR

Posted in JavaScript, Software, Web, internet, programming on April 24, 2007 by Joey

To specify an “OR” condition in JavaScript regular expressions, use the pipe character – |.

var StringToTest = “abc”;
var IsFound = /a|b/.test(StringToTest);
alert(IsFound);

The above statement returns true if ‘a’ or ‘b’ is in the test string.