Free Book Excerpt -- Lotus Notes and Domino 6 Development (3)

Objects as Properties of Objects

It is also possible to create objects as properties to other objects. This means associating an object's property with another object. For example, let's create a company object:

function company(name, city, state){
   this.name = name;
   this.city = city;
   this.state = state;
}

Now let's create two new instances of the company object:

company1 = new company("Fancy Bank","Bedford","MA");
company2 = new company("Emerging Innovations","Stoneham","MA");

Using these two new company objects, we can easily create the following new contact objects adding the company's properties to each:

contact1 = new contact("Mabel Chin","(781) 333-5252","Estate Attorney","September 17",company1.coname,company1.city,company1.state);
contact2 = new contact("Silke Hase","(781) 384-5470","Development Manager","May 5",company2.coname,company2.city,company2.state);

By making the company properties part of the contact object, it is now easy to refer to the company properties for each contact object:

contact2.coname

Adding Methods to Objects

Methods can be added to an object's definition by creating functions that define the method. Methods are merely functions associated with an object. Let's add a method called displaycontact to the contact object that will print the contact's name, phone, title, birthday, company, city, and state to a document window:

Function displaycontact(contact) {
document.write("Contact Name:t" + this.name + "<BR>");
document.write("Telephone:t" + this.telephone + "<BR>");
document.write("Title:tt" + this.title + "<BR>");
document.write("Birthday:t" + this.birthday + "<BR>");
document.write("Company:t" + this.coname + "BR>");
document.write("City:tt" + this.city + "<BR>");
document.write("State:tt" + this.state + "<BR>");
}

To use this method, it must be included in the contact object's definition:

function contact(name,telephone,title,birthday,coname,city,state){
this.name = name;
this.telephone = telephone;
this.title = title;
this.birthday = birthday;
this.coname = coname;
this.city = city;
this.state = state;
this.displaycontact = displaycontact;
}

The following command would be used to output contact2's information to the document window:

contact2.displaycontact();

Figure 16.17 shows what the output looks like in a document window.

Figure 16.17
The contact2 object's properties output to the browser.


Chapter 17: Real-World JavaScript Examples

Utility 5: validateDate()

Utility 5, validate(date(), validates the content of a date field and checks to make sure that what is entered is a date using a separator such as a period. Table 17.7 explains this utility's purpose, how to call it, its parameters and its code.

Table 17.7 About the validateDate() Function
Purpose:
Validates the contents of a date field to make sure that what was entered is actually a date. If what is entered is not a valid date, an error message is presented to the user.
How to call it:
validateDate(document.forms[0], 'myDateField', 'My Date Field Label')
Parameters:
form is the pointer to the form object
dateFieldName is the name of the date field that needs to be validated.
fieldLabel is the name of the date field's label.
Code:

function validateDate(form, dateFieldName,fieldLabel){
var checkstr = "0123456789";
var DateField = fieldName;
var Datevalue = "";
var DateTemp = "";
//Change the separator to reflect what your date field uses to separate the 
date's month, year, and day.
var separator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
err = 0;
DateValue = DateField.value;
     for (i = 0; i < DateValue.length; i++) {
          if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
                   DateTemp = DateTemp + DateValue.substr(i,1);
            }
   }
     DateValue = DateTemp;
     if (DateValue.length == 6) {
            DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
     if (DateValue.length != 8) {
            err = 19;}
            year = DateValue.substr(4,4);
     if (year == 0) {
            err = 20;
     }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22; 
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") 
|| (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") 
|| (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 is entered, no error, delete the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; separator = "";
   }
   /* if no error, write the date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
    DateField.value = day + separator + month + separator + year;
   }
   else {
      alert("The date entered is incorrect!");
      DateField.select();
      DateField.focus();
   }
} 



Page 1 | Page 2

-ADS BY GOOGLE

SearchWindowsServer

Search400

  • iSeries tutorials

    Search400.com's tutorials provide in-depth information on the iSeries. Our iSeries tutorials address areas you need to know about...

  • V6R1 upgrade planning checklist

    When upgrading to V6R1, make sure your software will be supported, your programs will function and the correct PTFs have been ...

  • Connecting multiple iSeries systems through DDM

    Working with databases over multiple iSeries systems can be simple when remotely connecting logical partitions with distributed ...

SearchDataCenter

SearchContentManagement

Close