Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | 1 Comment »
Posted 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();
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | 2 Comments »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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.
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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.
Filed under: Internet, JavaScript, Programming, Software, Web | Leave a Comment »
Posted 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 [...]
Filed under: Internet, JavaScript, Programming, Software, Web | 5 Comments »
Posted 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.
Filed under: Internet, JavaScript, Programming, Software, Web | 1 Comment »