Why you should stop using onload method.

8 Comments

onload1 276x300 Why you should stop using onload method.

Onload

It is amazing that many web developers still use onload method embedded in the BODY tag, like


 <HEAD>
 <BODY onload="document.contact.userID.focus();">
  <form name="contact">
   <input type="text" name="userID" >
  </form>
  </BODY>
 </HEAD>

There are two problems with this line of code.

  1. Embedding code (behavior) with HTML (structure) makes it difficult to maintain the page.
  2. onload() method will executes only after the page is fully displayed.
  3. Let’s see how to fix these problems. More

article clipper vert Why you should stop using onload method.
 

Nine Must Have JavaScript Tools For Every Web Developer

2 Comments

Almost all major web development projects include some javascript. Developing Javascript may be easy for some but working around various browsers’ incompatiblities makes it a bit more tedious chore. As a developer I always welcome the opportunity to use development tools that may help in the development process. Following is a list of some useful Javascript tools that I believe every web programmer should have at his or her disposal.
More

article clipper vert Nine Must Have JavaScript Tools For Every Web Developer
 

How to display RSS feed on your website.

2 Comments

rss m How to display RSS feed on your website.
Image credit:Kyle Wegner

OK, admit this. You have a nice-looking website. The color and the graphics look pleasing
to visiting eyes. But you do not get a lot of loyal visitors. The problem may not be your design,
it may be the static content that is turning the visitors off. If you do not update your website regularly, you may not attract enough repeat visitors. One way to juice up your website is to display RSS feeds from other relevant and reputable websites. Don’t know how to incorporate the RSS feeds into your website? Here is what you need: More

article clipper vert How to display RSS feed on your website.
 

Validate U.S Phone Numbers using JavaScript Regular expression.

15 Comments

279804967 668397cde9 m Validate U.S Phone Numbers using JavaScript Regular expression.
Photo credit: aussiegall

Continuing with our JavaScript regular expression series today we will discuss JavaScript regular expression to validate U.S phone number. Previously we talked about validating email , Social Security number and zip code using JS regex.

In today’s post I’ll show you how to use JavaScript regular expression to validate U.S phone number.


Although in this article we are discussing U.S phone number format I am sure this can be applied to other phone number formats with little or no change.

Let’s begin by looking at the JavaScript code.


function validatePhoneNumber(elementValue){
var phoneNumberPattern = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/;
return phoneNumberPattern.test(elementValue);
}

Explanation:

The argument to this method is the phone number you want to validate.

In the method body we define a variable (‘phoneNumberPattern’) and assign a regular expression to it.

Phone Number format: The regular expression for phone number is More

article clipper vert Validate U.S Phone Numbers using JavaScript Regular expression.
 

Validate Zip code using JavaScript Regular expression

7 Comments

zipcode 300x199 Validate Zip code using JavaScript Regular expression

Zip Code

Continuing with my series of JavaScript regular expression today we will see how easy it is to validate zip code using regular expression in JavaScript. Previously we talked about validating email and Social Security number using JS regex.

In today’s post I’ll show you how to use JavaScript regular expression to validate zip code. Let me first show you the JS code.


function validateZipCode(elementValue){
    var zipCodePattern = /^\d{5}$|^\d{5}-\d{4}$/;
     return zipCodePattern.test(elementValue);
}

Explanation:

The argument to this method is the zip code you want to validate.

In the method body we define a variable (‘zipCodePattern’) and assign a regular expression to it.

ZipCode format: The regular expression for zip code is

More

article clipper vert Validate Zip code using JavaScript Regular expression
 

How to validate Social Security Number (SSN) using JavaScript Regular Expressions

2 Comments

When I wrote the JS regular expression article last week I had no idea that it will attract so many visitors. Not only did so many people read this post, many more visited my blog searching for other regular expressions. So I decided to write few more posts about JavaScript regular expressions and how to use them to validate Social Security number, zip code, phone number and numeric data. I’ll divide these into smaller posts discussing one regular expression per post to keep it simple for the readers.

In today’s post I’ll show you how to use JavaScript regular expression to validate Social Security number. Let me first show you the JS code.

    function validateSSN (elementValue){
       var  ssnPattern = /^[0-9]{3}\-?[0-9]{2}\-?[0-9]{4}$/;
       return ssnPattern.test(elementValue);
   }

Explanation:

The argument to this method is the social security number you want to validate.

In the method body we define a variable (‘ssnPattern’) and assign a regular expression to it.

SNN format: The regular expression for social security number (SSN) is

More

article clipper vert How to validate Social Security Number (SSN) using JavaScript Regular Expressions
 

Validate email address using JavaScript regular expression

91 Comments

Last month I wrote about regular expressions in Java, today I’ll show you how to use regular expression in JavaScript to validate email address.

Here is the code to validate email address in JavaScript using regular expression.

function validateEmail(elementValue){
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
   return emailPattern.test(elementValue);
 }

Explanation:

The argument to this method is the email address you want to validate.
In the method body we define a variable (‘emailPattern’) and assign a regular expression to it.

Email format: The regular expression for email is
More

article clipper vert Validate email address using JavaScript regular expression
 

Set focus on the first text field of HTML form.

1 Comment

A HTML form is a very good channel that you can offer to your visitors to contact you. An aesthetically appealing form encourages user to fill and submit it. It is equally important to make the form as user friendly as possible. Today I’ll share a small JavaScript function that will set the focus on the first editable text field of your form. So when the web page loads the users don’t have to click on the text field. They can just start typing as soon as the form loads.

Here is the JavaScript code.

function setFocus(){
 var flag=false;
 for(z=0;z<document.forms.length;z++){
  var form = document.forms[z];
  var elements = form.elements;
  for (var i=0;i<elements.length;i++){
    var element = elements[i];
    if(element.type == 'text' &&
      !element.readOnly &&
      !element.disabled){
      element.focus();
	  flag=true;
     break;
    }
  }
  if(flag)break;
 }
}

How to use this script.

Save the script in a text file and name it focus.js.

To use script copy and paste following line into the header section of your form HTML file.

<script type=”text/javascript” src=”focus.js”>

If you saved the focus.js to a separate directory, include the directory with the file name (like src=”js/focus.js”).

Now add

onLoad=”setFocus();”

to the body tag.
Your body tag will become

<body onLoad=”setFocus(‘contactForm’);” >

Now when your page loads the focus will be on the first editable textfield on your form.Click here to see an example.

article clipper vert Set focus on the first text field of HTML form.
 

Sort numbers in Javascript array

4 Comments

numbers Sort numbers in Javascript array
Sorting an array in Javascript is a snap.
You create an array and then call sort() method on that array. The Javascript sort() method sorts an array in lexicographical order. This method is very useful in sorting alphanumeric values of an array. However, sort() will not work if the array consists of numeric values. Because the alphabetical order of numbers is different from their numeric order the sorted array may not be in the order you are expecting. For example, in a dictionary sort the number “11″ would come before number “5″. This may not be the result that you are looking for.
Here is an example

var ages = new Array (23,6,2,16,48,9,6);
ages.sort();

The array will become (16,2,23,48,6,6,9)

Fortunately, the work around for this problem is quite simple.
More

article clipper vert Sort numbers in Javascript array
 

Compress JavaScript and CSS files to optimize website speed.

3 Comments

Referencing external JavaScript and/or CSS files from a web page considerably increases the load time. As a rule of thumb remember that each external file requires additional HTTP request by the browser, hence the time to render a page will increase. Every web developer worth their salt should make optimization their priority. Fortunately, since this is such a common issue many people have thought about it and came up with solutions.
Below is a list of few of these solutions to optimize your website by compressing JavaScript and CSS files.

More

article clipper vert Compress JavaScript and CSS files to optimize website speed.